Trouble reading next int
so i failed on my last question -__-
So I want a program to read integers from a text file. The data file looks
like this:
5 4
3 5
4 5
3 2
(ignore the spaces between the lines)
The first number of each row is the par, and the second number is the
score. Because 5 - 4 = 1, the score is one under par (which is a birdie).
My code for the first line:
in = new Scanner (dataFile);
System.out.println("Golf Scores: ");
String line1 = in.nextLine();
System.out.println("Score 1: " + line1);
int par1 = in.nextInt();
System.out.println("Par is: " + par1);
When I run it, I get this:
Golf Scores: Score 1: 5 4 Par is: 3
So the par and score shows up correctly. However, the displayed par shows
the par of the next line from the text file. I want it to show "5" again.
I tried putting the in.nextInt before the in.nextLine, but when I tried
that I got this
Golf Scores: Score 1: 4 Par is: 5
No comments:
Post a Comment