Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. import java.io.FileNotFoundException;
  2. import java.util.*;
  3.  
  4. public class MapCount {
  5. public static void main(String[] args) {
  6. Scanner input = new Scanner(System.in);
  7.  
  8. String key = "";
  9. int key2 = 0;
  10. int T = 0;
  11. int K = 0;
  12. boolean i = true;
  13. int parsedLines = 0;
  14. int failedParsedLines = 0;
  15. String nextLine = "";
  16. Map<Integer, Integer> map = new TreeMap<>();
  17.  
  18. try{
  19. System.out.print("Please enter a text name: ");
  20. String f = input.nextLine();
  21. java.io.File file = new java.io.File(f);
  22. String path = file.getPath();
  23. Scanner scan = new Scanner(file);
  24. if (i = true){
  25. while (scan.hasNextLine()) {
  26. try {
  27. nextLine = scan.nextLine();
  28. key = nextLine;
  29. key2 = Integer.parseInt(key);
  30. if (!map.containsKey(key2))
  31. map.put(key2, 1);
  32. else
  33. map.put(key2, map.get(key2) + 1);
  34. Integer ii = Integer.parseInt(nextLine);
  35. parsedLines++;
  36. T = T + ii;
  37. K++;
  38. }
  39. catch(NumberFormatException ex){
  40. System.out.println("Cannot parse string as integer: " + nextLine);
  41. failedParsedLines++;
  42. }
  43. }
  44. }
  45. }
  46.  
  47. catch(FileNotFoundException ex){
  48. System.out.print("File not found.");
  49. System.exit(1);
  50. }
  51. int O = T / K;
  52. System.out.println("Number of parsable lines: " + parsedLines);
  53. System.out.println("Number of unparsable lines: " + failedParsedLines);
  54. System.out.println("Average value: " + O);
  55. int k = Collections.max(map.values());
  56. for(Map.Entry<Integer, Integer> entry : map.entrySet()){
  57. if (entry.getValue() == k){
  58. System.out.print(entry.getKey() + " ");
  59. }
  60. }
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement