1

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.

kcode
  • 13
  • 2
  • what data are you trying to read in which variable? – Stultuske Dec 06 '19 at 07:04
  • I'm trying to read the names into a String array, the xxx-xxx-xx IDs into another String array, and the numbers following that into an integer array. – kcode Dec 06 '19 at 07:08
  • Why do you keep track of the students' attributes in separate lists? You can create a class `Student` that holds all the data. That's how you use Java, by the way... – deHaar Dec 06 '19 at 07:13
  • I suppose I should've clarified that this is for an assignment and my professor asked for the attributes to be stored in arrays. – kcode Dec 06 '19 at 07:20

3 Answers3

0

Look at this code:

    studentList[i] = fileIn.nextLine();
    idList[i] = fileIn.nextLine();
    scoreList[i] = fileIn.nextInt();

You are reading line by line.

But 111-22-3333 100 90 80 70 60 88 is a single line (which is read into idList[i]).

You need to change your code in a way, that you read 111-22-3333 100 90 80 70 60 88 as a line, and then split the line into two (or more, that's up to you) segments, that you process further to get ID and scores.

Moritz Petersen
  • 12,902
  • 3
  • 38
  • 45
  • Yup I've outputted the arrays to troubleshoot and I noticed this, I'm just struggling to figure out how to split the line of numbers into segments. Thank you for your response. – kcode Dec 06 '19 at 07:25
  • @kcode see [How to split a string in Java](https://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java) – Abra Dec 06 '19 at 07:26
0

As an alternate to Moritz Petersens answer, for minimal changes to your code, you can use the next() method instead of nextLine() method to get the idList() by changing your while loop as follows;

    studentList[i] = fileIn.nextLine();
    idList[i] = fileIn.next();
    scoreList[i] = fileIn.nextLine();
    i++;

This will store the first token (Your ID) into the idList array and the remaining part of that line into the scoreList array. Which I assume you are expecting to happen here.

Refer: https://www.tutorialspoint.com/java/util/scanner_next.htm

Hope this helps!


UPDATE

I just observed that your scoreList array is of type int[].

Based on that, above fix will not work.

You will need to create a temporary string which takes all scores and then split it to put the appropriate integers into the score array. Also, create a new integer for tracking index of the scoreList array.

Something like;

int scoreIndex = 0;
while(fileIn.hasNext() && i < studentList.length){
    studentList[i] = fileIn.nextLine();
    idList[i] = fileIn.next();
    String scores = fileIn.nextLine();
    String[] splitScores = scores.trim.split(" ");
    for (String a : splitScores) {
        scoreList[scoreIndex] = Integer.parseInt(a);
        scoreIndex++;
    }
    i++;
}

Also, since you are using a primitive array dataType for scoreList array, make sure that the size of the array is number_of_subjects*number_of_students.

In your example, 6*3.

Shivam Puri
  • 1,578
  • 12
  • 25
  • Believe it or not I have tried this! What I found is that the first integer definitely gets picked up in my third array, but then I run into another error. Thanks for the resource though, I will read. :) – kcode Dec 06 '19 at 07:32
  • Thank you for your update this is very helpful, I will also try this method! Much appreciated. – kcode Dec 06 '19 at 07:56
0

You need to make the following changes to your code

studentList[i] = fileIn.nextLine();
String id_score = fileIn.nextLine();
String[] split = id_score.split(" ",2);
idList[i] = split[0]; 
scoreList[i] = split[1];

.split() will separate the id and score in the second line.

Sunil
  • 364
  • 1
  • 14
  • Made some adjustments and this worked for me, thank you! I appreciate it. – kcode Dec 06 '19 at 07:39
  • Definitely upvoted! As I'm a new user under 15 rep still, it tells me it hasn't been publicly changed but was recorded. If it wasn't, I will upvote again. :) – kcode Dec 06 '19 at 08:00