Hello I was wondering if I could get some help. I've been tasked with reading from a file and storing the information into arrays. I have three arrays meant to pick up the data, which for example looks like this:
Smith Jr., Joe
111-22-3333 100 90 80 70 60 88
Jones, Bill
111-11-1111 90 90 87 88 100 66
Brown, Nancy
222-11-1111 100 100 100 100 100 100
My code for reading the data is this so far:
public static void readFile(String[] studentList, String[] idList, int[] scoreList) throws IOException {
String fileName;
System.out.println("Enter file name and location if necessary: ");
fileName = KB.nextLine();
File f1= new File(fileName); // "C:\Users\User\Desktop\final.txt"
Scanner fileIn = new Scanner(f1);
int i = 0 ;
if (f1.length() == 0) {
System.out.println("File is empty");
}
while(fileIn.hasNext() && i < studentList.length){
studentList[i] = fileIn.nextLine();
idList[i] = fileIn.nextLine();
scoreList[i] = fileIn.nextInt();
i++;
}
}
When I go to output the code though, I get this error:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at managestudent.ManageStudent.readFile(ManageStudent.java:88)
at managestudent.ManageStudent.main(ManageStudent.java:36)
I've tried troubleshooting and what seems to be happening is that my second array stores the entire line and that my integers aren't getting read and stored into my third array.
I tried to set my second array as a long to pick up the numbers like 111-111-111 and differentiate from the integers but that gave me another error which seemed to be that my long array wasn't picking up anything from the second lines at all.
Any suggestions?
Thank you kindly for your time.