Unsure if the spacing in the text file is causing the error or something else I didn't see. Just like the title says, I keep getting the array index out of bounds exception. I have no idea why, any help is appreciated.
For better reference (I copy and paste the text file into a Pastebin): https://pastebin.com/reYraNpY
public static void main(String[] args) throws FileNotFoundException
{
Map<String, Integer> sort = storeText();
System.out.println("State: \t\t Population:");
for (Map.Entry<String, Integer> key : sort.entrySet())
{
System.out.printf("%-20s %d%n", key.getKey(), key.getValue());
}
}
public static Map<String, Integer> storeText() throws FileNotFoundException
{
Map<String, Integer> map = new HashMap<String, Integer>();
FileInputStream file = new FileInputStream("Assignment1CData.txt");
Scanner input = new Scanner(file);
while (input.hasNextLine())
{
String[] trim = input.nextLine().split(",");
String[] trim2 = trim[1].split(" ");
String state = trim2[0].replace(" ", "");
Integer totalPopulation = Integer.parseInt(trim2[1]);
map.put(state, map.getOrDefault(state, map.getOrDefault(state, 0) + totalPopulation));
}
return map;
}