PDA

View Full Version : [JAVA] Variable might not have been initialized?


Crashfan3
02-28-2012, 03:49 PM
Hi,

I'm trying to write a program that reads a list of golf scores from a text file, and then stores each golfer's score to memory.
The scores are in the text file like this:
3 4 3 2 5 (18 lines like this, where the first number is par, the others are each golfer's score)
My program reads each hole just fine but I can't figure out how to properly store each golfer's score and add them up to get a final score so that I can determine who wins.

My loop looks like this:

// Print each golfer's score individiually
while (holeScan.hasNext())
{
int par;
int g1score, g2score, g3score, g4score;
System.out.println("PAR : " + holeScan.next());
par = Integer.parseInt(holeScan.next());
System.out.println("Golfer1: " + holeScan.next());
g1score = g1score + Integer.parseInt(holeScan.next());
System.out.println("Golfer2: " + holeScan.next());
g2score = g2score + Integer.parseInt(holeScan.next());
System.out.println("Golfer3: " + holeScan.next());
g3score = g3score + Integer.parseInt(holeScan.next());
System.out.println("Golfer4: " + holeScan.next());
g4score = g4score + Integer.parseInt(holeScan.next());
}


When I compile, I'm being told that each of the variables above "might not have been initialized." When I set them all equal to zero, I get a short set of incorrect results followed by an exception.
http://i.imgur.com/A9GxG.png

I'm not begging for someone to write my code for me, but I can't get my head around this little piece so I thought I'd come here for help since this is due tomorrow morning.

If you need the rest of my code, just ask.
Thanks!!

rushyrulz
02-28-2012, 03:59 PM
I'm no expert, but have you considered storing each set of information in a separate array and looping through them to obtain the finals?

Crashfan3
02-28-2012, 04:35 PM
We haven't covered arrays yet (I think that's next week actually), so I don't know how to do that, and it's probably not how the instructor wants me to write the code.

rushyrulz
02-28-2012, 04:52 PM
you're reading text files before you've started arrays? what in the ****? I guess you could just use separate while loops for each player and total the scores as you go along

should be like, while hole <=18, get par and scores, add current par and scores to totalpar and totalscore variables and you should be able to print that out in the end.

cry4eternity
02-28-2012, 07:21 PM
System.out.println("PAR : " + holeScan.next());
This line reads in the first value, "3", and prints it.


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


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


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.


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


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:


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


System.out.println("Golferx: " + holeScan.next());

megamon88
02-28-2012, 07:45 PM
Also, make sure you initialize your variables first (like you said, set them to 0 in your declaration). When you do g1score = g1score + Integer.parseInt(holeScan.next());, then g1 score already has to have a value since it is being referenced, even though it may be 0 at the time. The reason you were getting the wrong total/errors was because of what cry4eternity posted above.

trumaestro
02-28-2012, 08:08 PM
Also, make sure you initialize your variables first (like you said, set them to 0 in your declaration).

And ya gotta do this outside of your loop or else you're just redefining the variables every time the loop executes

megamon88
02-28-2012, 08:12 PM
Oh yeah, forgot to mention that :P

Crashfan3
02-29-2012, 11:24 AM
Posting from CS161 class. My code met the expectations. Thanks guys! ^^