How to read from textfile and store in arraylist?
Question: Add (to the ArrayList) all information about a coursework or research student (except the overall mark and the grade) by reading it from another text file and determine the student’s overall mark and grade.
This is my text file: Screenshot of textfile
Here is my code for the question:
public static void addInfo() { Scanner in = new Scanner(System.in); System.out.println("Enter the file name (Coursework or Research): "); String fileName = in.nextLine(); String studTitle, firstName, lastName; long studId; int dobDay, dobMonth, dobYear; Student st1 = null; File f = new File(fileName); try(Scanner sc = new Scanner(f)) { while(sc.hasNextLine()) { studTitle = sc.nextLine(); firstName = sc.nextLine(); lastName = sc.nextLine(); studId = Long.parseLong(sc.nextLine()); String[] dob = sc.nextLine().split("/"); dobDay = Integer.parseInt(dob[0]); dobMonth = Integer.parseInt(dob[1]); dobYear = Integer.parseInt(dob[2]); if(type == 'C') { double a1 = Double.parseDouble(sc.nextLine()); double a2 = Double.parseDouble(sc.nextLine()); double a3 = Double.parseDouble(sc.nextLine()); double practical = Double.parseDouble(sc.nextLine()); double exam = Double.parseDouble(sc.nextLine()); st1 = new Coursework(studTitle, firstName, lastName, studId, dobDay, dobMonth, dobYear, a1, a2, a3, practical, exam); } if(type == 'R') { double proposal = Double.parseDouble(sc.nextLine()); double oralP1 = Double.parseDouble(sc.nextLine()); double oralP2 = Double.parseDouble(sc.nextLine()); double thesis = Double.parseDouble(sc.nextLine()); st1 = new Research(studTitle, firstName, lastName, studId, dobDay, dobMonth, dobYear, proposal, oralP1, oralP2, thesis); } assert st1 != null; st1.computeResult(); st1.setGrade(); studentList[totalStudent] = st1; totalStudent++; }
Error:
Exception in thread "main" java.lang.NumberFormatException: For input string: "Mr Calvin Chen 153392 22/10/1990 65 75 85 15 50" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Long.parseLong(Long.java:589) at java.lang.Long.parseLong(Long.java:631) at a2.Client.addInfo(Client.java:134) at a2.Client.main(Client.java:74)
Is my input for textfile perhaps wrong?