Go Back   Flash Flash Revolution > General Discussion > Technology

Reply
 
Thread Tools Display Modes
Old 02-28-2012, 03:49 PM   #1
Crashfan3
FFR Player
 
Crashfan3's Avatar
 
Join Date: Nov 2006
Location: Jefferson, Ore.
Age: 32
Posts: 2,937
Send a message via AIM to Crashfan3 Send a message via Skype™ to Crashfan3
Default [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());
}
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.


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!!
__________________
Crashfan3 is offline   Reply With Quote
Old 02-28-2012, 03:59 PM   #2
rushyrulz
Digital Dancing!
Retired StaffFFR Simfile AuthorFFR Music ProducerD7 Elite KeysmasherFFR Veteran
 
rushyrulz's Avatar
 
Join Date: Feb 2006
Location: 80 billion club, NE
Age: 31
Posts: 12,979
Default 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?
__________________


rushyrulz is offline   Reply With Quote
Old 02-28-2012, 04:35 PM   #3
Crashfan3
FFR Player
 
Crashfan3's Avatar
 
Join Date: Nov 2006
Location: Jefferson, Ore.
Age: 32
Posts: 2,937
Send a message via AIM to Crashfan3 Send a message via Skype™ to Crashfan3
Default 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.
__________________
Crashfan3 is offline   Reply With Quote
Old 02-28-2012, 04:52 PM   #4
rushyrulz
Digital Dancing!
Retired StaffFFR Simfile AuthorFFR Music ProducerD7 Elite KeysmasherFFR Veteran
 
rushyrulz's Avatar
 
Join Date: Feb 2006
Location: 80 billion club, NE
Age: 31
Posts: 12,979
Default 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..
rushyrulz is offline   Reply With Quote
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
Old 02-28-2012, 07:45 PM   #6
megamon88
aka Assertive
Retired StaffFFR Simfile AuthorFFR Music ProducerFFR Veteran
 
megamon88's Avatar
 
Join Date: Feb 2006
Location: Waterloo, ON
Age: 29
Posts: 2,567
Default 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.
__________________
megamon88 is offline   Reply With Quote
Old 02-28-2012, 08:08 PM   #7
trumaestro
I don't get no respect
FFR Simfile AuthorFFR Veteran
 
trumaestro's Avatar
 
Join Date: Jun 2006
Age: 32
Posts: 1,332
Default Re: [JAVA] Variable might not have been initialized?

Quote:
Originally Posted by megamon88 View Post
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
trumaestro is offline   Reply With Quote
Old 02-28-2012, 08:12 PM   #8
megamon88
aka Assertive
Retired StaffFFR Simfile AuthorFFR Music ProducerFFR Veteran
 
megamon88's Avatar
 
Join Date: Feb 2006
Location: Waterloo, ON
Age: 29
Posts: 2,567
Default Re: [JAVA] Variable might not have been initialized?

Oh yeah, forgot to mention that :P
__________________
megamon88 is offline   Reply With Quote
Old 02-29-2012, 11:24 AM   #9
Crashfan3
FFR Player
 
Crashfan3's Avatar
 
Join Date: Nov 2006
Location: Jefferson, Ore.
Age: 32
Posts: 2,937
Send a message via AIM to Crashfan3 Send a message via Skype™ to Crashfan3
Default Re: [JAVA] Variable might not have been initialized?

Posting from CS161 class. My code met the expectations. Thanks guys! ^^
__________________
Crashfan3 is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 08:54 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright FlashFlashRevolution