View Single Post
Old 02-28-2012, 07:21 PM   #5
cry4eternity
~ added for cuteness
FFR Simfile AuthorFFR Veteran
 
cry4eternity's Avatar
 
Join Date: Jan 2007
Location: Maryland
Age: 31
Posts: 979
Send a message via AIM to cry4eternity
Default Re: [JAVA] Variable might not have been initialized?

Code:
System.out.println("PAR    : " + holeScan.next());
This line reads in the first value, "3", and prints it.

Code:
par = Integer.parseInt(holeScan.next());
This line reads in the next value, "4", and stores it in par.

Code:
System.out.println("Golfer1: " + holeScan.next());
This line prints the next value, "3".

Code:
g1score = g1score + Integer.parseInt(holeScan.next());
This line reads the next value, "2", and adds it to the uninitialized variable, g1score. This line and others like it is where your warnings come from.

Code:
System.out.println("Golfer2: " + holeScan.next());
This line reads the next value, "5" and prints it.

Code:
g2score = g2score + Integer.parseInt(holeScan.next());
Assuming this scanner is only reading a single line, there is no next token and the scanner throws an error.

From this, you can probably see what you're doing wrong. When you call the scanner's next() method, it moves the current pointer forward in the file to the next token. Don't try to read the variable twice from the file. Do either one of these:

Code:
gxscore = Integer.parseInt(holeScan.next());
System.out.println("Golferx: " + gxscore);
Code:
System.out.println("Golferx: " + holeScan.next());
__________________

I'm retired
cry4eternity is offline   Reply With Quote