Pete1

Untitled

Apr 2nd, 2016
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. package CollectionsHomework;
  2.  
  3. import java.util.LinkedHashMap;
  4. import java.util.Map;
  5. import java.util.Scanner;
  6.  
  7. public class Problem10CardFrequencies {
  8.  
  9. public static void main(String[] args) {
  10. @SuppressWarnings("resource")
  11. Scanner input = new Scanner(System.in);
  12. String[] arrStr = input.nextLine().split("\\W+");
  13.  
  14. Map<String, Integer> wordCounterMap = new LinkedHashMap<String, Integer>();
  15.  
  16. Integer oldValue = 0;
  17. for (String wordCount : arrStr) {
  18. oldValue = wordCounterMap.get(wordCount);
  19. if (oldValue == null)
  20. oldValue = 0;// first time is always null
  21.  
  22. wordCounterMap.put(wordCount, oldValue + 1);
  23.  
  24. }
  25.  
  26. for (Map.Entry<String, Integer> entry : wordCounterMap.entrySet()) {
  27.  
  28. System.out.printf("%s - > %.2f%%", (String) entry.getKey(),
  29. (((double) entry.getValue() / arrStr.length) * 100));
  30. System.out.println();
  31.  
  32. }
  33. }
  34.  
  35. }
Add Comment
Please, Sign In to add comment