简体   繁体   中英

How can I check the next line without eating the next integer?

I'm grappling with a specific problem, but seeing as this is technically homework and I want to learn what I'm doing wrong, I would love a more general solution. Some caveats: I have to use the scanner class, and my data is not in an array or anything. I know from reading around on the site that BufferedReading is preferred. From what I've read of it, I think I would prefer it too. But that's not what I'm allowed to work with here.

I'm trying to read from a data file and then do some stuff with that file. The data goes like this:

1234 55 64 75
1235 44 32 12
...
nnnn xx yy zz
0
2234 47 57 67
2235 67 67 67
...
nnnn xx yy zz
0

Each line is an ID followed by three grades. Each class is terminated by a zero line and then the while loop starts from the top:

while (classContinues == true) {                   
//get the student's data and assign it in the program
studentID = inputFile.nextInt();
programGrade = inputFile.nextInt();
midtermGrade = inputFile.nextInt();
finalGrade = inputFile.nextInt();

// in here I'm doing other stuff but I don't need help with that

// check if the class has more students
if (inputFile.nextInt() == 0) {
    classContinues = false;
} else {
    classContinues = true;
    inputFile.nextLine(); // eat the rest of the line
}
}

Now, when you run the code like this, it manages to print the output I want, but it skips every other row of data. Remove the inputFile.nextLine(); and it skips the second student ID and then messes up all the other output. So I guess what I'd like to know is what I'm doing wrong--how should I be checking for the next integer to be zero without eating the next student ID?

Below piece of code will jump out of the while loop when it comes to the first '0' from the input. That is why it cannot catch all the records.

if (inputFile.nextInt() == 0) {
    classContinues = false;
} else {
    classContinues = true;
    inputFile.nextLine(); // eat the rest of the line
}

And for nextInt() method, when it is called, it will return the current int value and point to the next one.

Try below while codes, it can get each line of the grade records. And I create an entity Named StudentGrade to store the record. And the For each loop will print out the records stored in the list.

    while (classContinues == true) {
        StudentGrade stu = new StudentGrade();
        // get the student's data and assign it in the program
        int id = 0;

        if ((id = inputFile.nextInt()) != 0) {
            stu.studentID = id;
        stu.programGrade = inputFile.nextInt();
        stu.midtermGrade = inputFile.nextInt();
        stu.finalGrade = inputFile.nextInt();
        studentGrades.add(stu);
        // in here I'm doing other stuff but I don't need help with that
        // check if the class has more students
        }
        else if (!inputFile.hasNext()) {
            classContinues = false;
        }
    }

    for (StudentGrade s : studentGrades) {
        System.out.println(s);
    }

input data:

1234 55 64 75
1235 44 32 12
1236 23 32 32
0
2234 47 57 67
2235 67 67 67
2236 23 23 2
0

output:

1234 55 64 75
1235 44 32 12
1236 23 32 32
2234 47 57 67
2235 67 67 67
2236 23 23 2

By the way, it'd better to use Mehmet's method to get the records, it's much easier and understandable.

PS This is my first answer in StackOverflow. Hope that it can help.

Store every line into a String variable, parse integers from that line and then assign them by reading it from that string, not from the line itself. So:

String nextLine;

while (classContinues)
{             
nextLine = inputFile.nextLine();

String[] tokens = nextLine.split(" ");

if(tokens.length == 1) //this means line has '0' character
    classContinues = false;
else
    {
    classContinues = true;

    studentID = tokens[0];
    programGrade = tokens[1];
    midtermGrade = tokens[2];
    finalGrade = tokens[3];

    // your stuff
    }
}

If there will be any kind of error that will show misleading results with this code, it is probably my bad because I don't know the rest of the project. So I posted a code similar to yours.

Also you have to do a null check to that String that you got from nextLine method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM