import java.io.*; import java.util.*; import java.text.*; public class 2DArray { int Mark[] [] = new int [100] [100]; // max numbers for a 3 by 4 array String[] Names = new String [4]; int count = 0; void read () throws IOException { BufferedReader reader = new BufferedReader (new FileReader ("Class.txt")); // reads the 5 students String line = null; while ((line = reader.readLine ()) != null) { Names [count] = (line); line = reader.readLine (); Mark [0] [0] = Integer.parseInt (line); line = reader.readLine (); Mark [0] [0] = Integer.parseInt (line); line = reader.readLine (); Mark [0] [0] = Integer.parseInt (line); line = reader.readLine (); Mark [0] [0] = Integer.parseInt (line); line = reader.readLine (); count++; } reader.close (); } void displayorg () throws IOException { System.out.println ("What has been read: "); for (int i = 1 ; i <= 3 ; i++) { for (int j = 1 ; j <= 4 ; j++) { System.out.print (Names [j]); System.out.print (Mark [i] [j] + "\t"); } System.out.println ("\n"); } } public static void main (String str[]) throws IOException { BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in)); 2DArray k = new 2DArray(); // The methods dont have to be static now k.read (); k.displayorg (); } }
My text file looks like this: A name of a student followed by his 4 marks
Caral
34
23
55
33
Katrina
87
78
98
87
Sam
54
65
76
56
Mathew
46
76
55
86
How can I properly read this off. It tells me:
java.lang.NumberFormatException: For input string: "Sam"
at java.lang.NumberFormatException.forInputString(Unk nown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at Array42.read(Array42.java:44)
at Array42.main(Array42.java:77)
I am also using Ready to Program java.... So scanners wont work most probably