Hello, I am practicing reading from a text-file into arrays. I have encountered a problem I am not able to solve.
I have a file called "nameage.txt":
Tom 44 Arnold 98 Susie 24 Johnny 17
My goal is to read this info into two different arrays, a string array called name, and an integerer array called age. Here is my attempt:
Please note the part I have commented out, it is the reading of the ages. If I don't have that part with me, the reading of the names goes perfectly fine and I have checked that the array is correct. But if I try to uncomment the one line and read in the ages aswell the program will compile but an exception is caught, can you guys see what is wrong with the code?import java.util.Scanner; import java.io.File; class nameAge { public static void main(String[] args) { String[] name = new String[4]; int[] age = new int[4]; try { Scanner reader = new Scanner(new File("nameage.txt")); String[] line = new String[2]; int i = 0; while (reader.hasNextLine()) { line = reader.nextLine().split(" "); name[i] = line[0]; //age[i] = Integer.parseInt(line[1]); i++; } } catch(Exception e) { System.out.println("Reading didn't work."); } } }