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

SerebroRaya

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?

Mehmet Sedat Güngör

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Java: How to check for next ints without going to the next line (Scanner)

From Dev

Java: How to check for next ints without going to the next line (Scanner)

From Dev

How can I align multi-line text next to an image without it wrapping underneath?

From Dev

How can I align the text with the text in the next line in console?

From Dev

How can I stop before reading the next line?

From Dev

How to check the next empty line in textpad and append the next input

From Dev

How to check if next line in text file is empty?

From Dev

Check the next line

From Dev

How can I select the next element using Nokogiri without calling "next_element" method?

From Dev

How to float left without wrapping to next line?

From Dev

How can I use the grep command to extract line which is the next line of greping line?

From Dev

How do I delete the next line in vim?

From Dev

How can I delete the last word in the current line, but only if a pattern occurs on the next line?

From Dev

How can I delete the last word in the current line, but only if a pattern occurs on the next line?

From Dev

How do i go to the next line without it adding in a <br/> tag using Notepad++

From Dev

Protractor: How can I get the next tag

From Dev

How can I select next element after $(this)?

From Dev

How can I remove the underscore next to the image?

From Dev

How can I read the next DataGridView row?

From Dev

How can I round the next number in a list?

From Dev

How can I round the next number in a list?

From Dev

How can I remove the underscore next to the image?

From Dev

How to use jQuery to check if element breaks to the next line?

From Dev

How can I use VBA to check the box next to a Pivot Table field to show the field in the table?

From Dev

On a web page, How can i create a horizontal scroll instead of having this wap to the next line?

From Dev

How can I force in javascript to execute a line before moving to next one?

From Dev

How can I use scanf to get input from the next line of input data?

From Dev

In Ruby, how can I print my next prompt on the same line after user input?

From Dev

How can i wait for a frame to perform an action before the code execute the next line?

Related Related

  1. 1

    Java: How to check for next ints without going to the next line (Scanner)

  2. 2

    Java: How to check for next ints without going to the next line (Scanner)

  3. 3

    How can I align multi-line text next to an image without it wrapping underneath?

  4. 4

    How can I align the text with the text in the next line in console?

  5. 5

    How can I stop before reading the next line?

  6. 6

    How to check the next empty line in textpad and append the next input

  7. 7

    How to check if next line in text file is empty?

  8. 8

    Check the next line

  9. 9

    How can I select the next element using Nokogiri without calling "next_element" method?

  10. 10

    How to float left without wrapping to next line?

  11. 11

    How can I use the grep command to extract line which is the next line of greping line?

  12. 12

    How do I delete the next line in vim?

  13. 13

    How can I delete the last word in the current line, but only if a pattern occurs on the next line?

  14. 14

    How can I delete the last word in the current line, but only if a pattern occurs on the next line?

  15. 15

    How do i go to the next line without it adding in a <br/> tag using Notepad++

  16. 16

    Protractor: How can I get the next tag

  17. 17

    How can I select next element after $(this)?

  18. 18

    How can I remove the underscore next to the image?

  19. 19

    How can I read the next DataGridView row?

  20. 20

    How can I round the next number in a list?

  21. 21

    How can I round the next number in a list?

  22. 22

    How can I remove the underscore next to the image?

  23. 23

    How to use jQuery to check if element breaks to the next line?

  24. 24

    How can I use VBA to check the box next to a Pivot Table field to show the field in the table?

  25. 25

    On a web page, How can i create a horizontal scroll instead of having this wap to the next line?

  26. 26

    How can I force in javascript to execute a line before moving to next one?

  27. 27

    How can I use scanf to get input from the next line of input data?

  28. 28

    In Ruby, how can I print my next prompt on the same line after user input?

  29. 29

    How can i wait for a frame to perform an action before the code execute the next line?

HotTag

Archive