Advertisement
Guest User

Untitled

a guest
May 19th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. package com.collin.election;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.util.*;
  6.  
  7. public class ElectionStats
  8. {
  9. private static int winningVotes = 0;
  10. private static int totalVotes = 0;
  11.  
  12. private static Map<String,Integer> headers = new HashMap<>();
  13.  
  14. private static int line = 0;
  15.  
  16. public static void main(String... args) throws FileNotFoundException, InterruptedException
  17. {
  18. File inputFile = new File("results14utf8.csv");
  19. Scanner scanner = new Scanner(inputFile);
  20.  
  21. scanner.nextLine();
  22.  
  23. while(scanner.hasNextLine())
  24. {
  25. List<String> splitLine = splitLineWithQuotes(scanner.nextLine());
  26. if(splitLine.get(4) != null && !splitLine.get(4).equals("n/a"))
  27. {
  28. if(splitLine.get(15) != null && !splitLine.get(15).isEmpty())
  29. {
  30. try
  31. {
  32. int score = Integer.parseInt(splitLine.get(15).replaceAll("\\,", ""));
  33. totalVotes += score;
  34. if (splitLine.get(21) != null && !splitLine.get(21).isEmpty())
  35. {
  36. winningVotes += score;
  37. }
  38. }
  39. catch(NumberFormatException e)
  40. {
  41.  
  42. }
  43. }
  44. }
  45. }
  46.  
  47. System.out.println("Total Votes: " + totalVotes);
  48. System.out.println("Winning Votes: " + winningVotes);
  49. }
  50.  
  51. private static List<String> splitLineWithQuotes(String line)
  52. {
  53. List<String> result = new ArrayList<>(23);
  54. StringBuilder builder = new StringBuilder();
  55. boolean inQuote = false;
  56. for(char c : line.toCharArray())
  57. {
  58. if(c == '"')
  59. {
  60. inQuote = !inQuote;
  61. }
  62. else
  63. {
  64. if (c == ',')
  65. {
  66. if (inQuote)
  67. {
  68. builder.append(c);
  69. } else
  70. {
  71. result.add(builder.toString());
  72. builder = new StringBuilder();
  73. }
  74. } else
  75. {
  76. builder.append(c);
  77. }
  78. }
  79. }
  80. return result;
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement