hash data


SUBMITTED BY: knightley

DATE: Sept. 3, 2015, 9:29 a.m.

FORMAT: Java

SIZE: 3.0 kB

HITS: 559

  1. import java.io.File;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.Scanner;
  7. public class HashData{
  8. public static void main(String[] args) throws Exception {
  9. if (args.length != 1) {
  10. System.out.println("2013ALL.csv");
  11. System.exit(1);
  12. }
  13. File input = new File(args[0]);
  14. List<String> attributes = readAttributes(input);
  15. if (attributes.size() > 0) {
  16. int index = 0;
  17. for (String attribute : attributes) {
  18. System.out.println(String.format("Attribute %d %s", index, attribute));
  19. index++;
  20. }
  21. System.out.println(
  22. String.format("What attribute would you like to summarize (0..%d)?",
  23. attributes.size() - 1));
  24. Scanner scanner = new Scanner(System.in);
  25. index = scanner.nextInt();
  26. if ((index >= attributes.size()) || (index < 0)) {
  27. System.out.println(String.format("Wrong attribute index %d", index));
  28. } else {
  29. System.out.println(
  30. String.format("Summarizing attribute %d (%s)", index, attributes.get(index)));
  31. sum(input, index);
  32. }
  33. }
  34. }
  35. private static List<String> readAttributes(File input) throws Exception {
  36. List<String> list = new ArrayList<String>();
  37. Scanner scanner = new Scanner(input);
  38. String line;
  39. while (scanner.hasNextLine()) {
  40. line = scanner.nextLine();
  41. if (line.startsWith("@data")) {
  42. break;
  43. }
  44. if (line.startsWith("@attribute")) {
  45. String[] parts = line.split(" ");
  46. list.add(parts[1]);
  47. }
  48. }
  49. return list;
  50. }
  51. private static void sum(File input, int index) throws Exception {
  52. Scanner scanner = new Scanner(input);
  53. Map<String, Integer> values = new HashMap<String, Integer>();
  54. String line, key;
  55. boolean dataStarted = false;
  56. while (scanner.hasNextLine()) {
  57. line = scanner.nextLine();
  58. if (line.startsWith("@data")) {
  59. dataStarted = true;
  60. } else if (dataStarted) {
  61. String[] parts = line.split(",");
  62. key = parts[index];
  63. if (values.containsKey(key)) {
  64. values.put(key, values.get(key) + 1);
  65. } else {
  66. values.put(key, 1);
  67. }
  68. }
  69. }
  70. for (Map.Entry<String, Integer> entry : values.entrySet()) {
  71. System.out.println(String.format("%s appeared %d times", entry.getKey(), entry.getValue()));
  72. }
  73. System.out.println(String.format("There were %d unique attributes values.", values.size()));
  74. }
  75. }

comments powered by Disqus