Advertisement
Guest User

Untitled

a guest
Nov 17th, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. import java.io.EOFException;
  2. import java.util.ArrayList;
  3.  
  4. public class FrequencyCounter {
  5.  
  6. ICharacterReader characterReader;
  7. int counter = 0;
  8. int counter2 = 0;
  9.  
  10. public FrequencyCounter(ICharacterReader charReader) {
  11. characterReader = charReader;
  12. boolean endOfStream = false;
  13. ArrayList<String> uniqueStrings = new ArrayList<>();
  14. ArrayList<Integer> noOfUniques = new ArrayList<>();
  15.  
  16. //loops until there are no more characters
  17. while (endOfStream == false) {
  18. try {
  19. char inputChar = '\n';
  20. String keyString = "";
  21.  
  22. //finds individual words
  23. while (inputChar != ' ') {
  24. inputChar = characterReader.GetNextChar();
  25. keyString += inputChar;
  26. }
  27.  
  28. //checks for existing versions of the same word
  29. boolean foundCopy = false;
  30. int copyIndex = 0;
  31. if (!uniqueStrings.isEmpty()) {
  32. for (int i = 0; i < uniqueStrings.size(); i++) {
  33. if (keyString.equals(uniqueStrings.get(i))) {
  34. foundCopy = true;
  35. copyIndex = i;
  36. }
  37. }
  38. }
  39.  
  40. //adds uniques to the arraylist and increments for non-uniques
  41. System.out.println(foundCopy);
  42. counter++;
  43. if (foundCopy == false) {
  44. uniqueStrings.add(keyString);
  45. noOfUniques.add(1);
  46. } else if (foundCopy == true) {
  47. int val = noOfUniques.get(copyIndex);
  48. noOfUniques.set(copyIndex, val++);
  49. counter2++;
  50. }
  51.  
  52. //catches end of stream exception
  53. } catch (EOFException e) {
  54. endOfStream = true;
  55. System.out.println("End of stream");
  56. }
  57. }
  58.  
  59.  
  60. //prints values
  61. for (int i = 0; i < uniqueStrings.size(); i++) {
  62. System.out.println(uniqueStrings.get(i) + " " + noOfUniques.get(i));
  63. }
  64. System.out.println(counter);
  65. System.out.println(counter2);
  66. System.out.println(uniqueStrings.size());
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement