02-28-2012, 03:49 PM | #1 |
FFR Player
|
[JAVA] Variable might not have been initialized?
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: Code:
// 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()); } 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!!
__________________
Twilight Sparkle's Ponyville Group Free Chiptune Streaming Radio! Thanks justin_ator and megamon88 for making my sig 20% cooler. |
02-28-2012, 03:59 PM | #2 |
Digital Dancing!
Join Date: Feb 2006
Location: 80 billion club, NE
Age: 31
Posts: 12,980
|
Re: [JAVA] Variable might not have been initialized?
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?
__________________
|
02-28-2012, 04:35 PM | #3 |
FFR Player
|
Re: [JAVA] Variable might not have been initialized?
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.
__________________
Twilight Sparkle's Ponyville Group Free Chiptune Streaming Radio! Thanks justin_ator and megamon88 for making my sig 20% cooler. |
02-28-2012, 04:52 PM | #4 |
Digital Dancing!
Join Date: Feb 2006
Location: 80 billion club, NE
Age: 31
Posts: 12,980
|
Re: [JAVA] Variable might not have been initialized?
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.
__________________
Last edited by rushyrulz; 02-28-2012 at 04:57 PM.. |
02-28-2012, 07:21 PM | #5 |
~ added for cuteness
|
Re: [JAVA] Variable might not have been initialized?
Code:
System.out.println("PAR : " + holeScan.next()); Code:
par = Integer.parseInt(holeScan.next()); Code:
System.out.println("Golfer1: " + holeScan.next()); Code:
g1score = g1score + Integer.parseInt(holeScan.next()); Code:
System.out.println("Golfer2: " + holeScan.next()); Code:
g2score = g2score + Integer.parseInt(holeScan.next()); 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 |
02-28-2012, 07:45 PM | #6 |
aka Assertive
Join Date: Feb 2006
Location: Waterloo, ON
Age: 29
Posts: 2,567
|
Re: [JAVA] Variable might not have been initialized?
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.
|
02-28-2012, 08:08 PM | #7 |
I don't get no respect
Join Date: Jun 2006
Age: 32
Posts: 1,332
|
Re: [JAVA] Variable might not have been initialized?
|
02-28-2012, 08:12 PM | #8 |
aka Assertive
Join Date: Feb 2006
Location: Waterloo, ON
Age: 29
Posts: 2,567
|
Re: [JAVA] Variable might not have been initialized?
Oh yeah, forgot to mention that :P
|
02-29-2012, 11:24 AM | #9 |
FFR Player
|
Re: [JAVA] Variable might not have been initialized?
Posting from CS161 class. My code met the expectations. Thanks guys! ^^
__________________
Twilight Sparkle's Ponyville Group Free Chiptune Streaming Radio! Thanks justin_ator and megamon88 for making my sig 20% cooler. |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
|
|