Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.EOFException;
- import java.util.ArrayList;
- public class FrequencyCounter {
- ICharacterReader characterReader;
- int counter = 0;
- int counter2 = 0;
- public FrequencyCounter(ICharacterReader charReader) {
- characterReader = charReader;
- boolean endOfStream = false;
- ArrayList<String> uniqueStrings = new ArrayList<>();
- ArrayList<Integer> noOfUniques = new ArrayList<>();
- //loops until there are no more characters
- while (endOfStream == false) {
- try {
- char inputChar = '\n';
- String keyString = "";
- //finds individual words
- while (inputChar != ' ') {
- inputChar = characterReader.GetNextChar();
- keyString += inputChar;
- }
- //checks for existing versions of the same word
- boolean foundCopy = false;
- int copyIndex = 0;
- if (!uniqueStrings.isEmpty()) {
- for (int i = 0; i < uniqueStrings.size(); i++) {
- if (keyString.equals(uniqueStrings.get(i))) {
- foundCopy = true;
- copyIndex = i;
- }
- }
- }
- //adds uniques to the arraylist and increments for non-uniques
- System.out.println(foundCopy);
- counter++;
- if (foundCopy == false) {
- uniqueStrings.add(keyString);
- noOfUniques.add(1);
- } else if (foundCopy == true) {
- int val = noOfUniques.get(copyIndex);
- noOfUniques.set(copyIndex, val++);
- counter2++;
- }
- //catches end of stream exception
- } catch (EOFException e) {
- endOfStream = true;
- System.out.println("End of stream");
- }
- }
- //prints values
- for (int i = 0; i < uniqueStrings.size(); i++) {
- System.out.println(uniqueStrings.get(i) + " " + noOfUniques.get(i));
- }
- System.out.println(counter);
- System.out.println(counter2);
- System.out.println(uniqueStrings.size());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement