Advertisement
zharry

Arraylist with Word Count

Oct 29th, 2017
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. General Idea:
  2.  
  3. sentence: input string as an array of words (split by space)
  4. arraylist: your arraylist of words (your custom class)
  5.  
  6. foreach string in sentence:
  7. found = false
  8. foreach word in arraylist:
  9. if word equals word:
  10. word.count++
  11. found = true
  12. break
  13. if !found
  14. arraylist.add(word)
  15.  
  16. My Code:
  17.  
  18. String[] sProc = input.trim().split(" ");
  19. for (int i = 0; i < sProc.length; i++) {
  20. boolean found = false;
  21. loop: for (int j = 0; j < strCount.size(); j++)
  22. if (sProc[i].equals(strCount.get(j).getString())) {
  23. strCount.get(j).inc();
  24. found = true;
  25. break loop;
  26. }
  27. if (!found)
  28. strCount.add(new StrCount(sProc[i]));
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement