Front Office Football Central  

Go Back   Front Office Football Central > Archives > FOFC Archive
Register FAQ Members List Calendar Mark Forums Read Statistics

Reply
 
Thread Tools
Old 08-12-2007, 06:04 AM   #1
Dutch
"Dutch"
 
Join Date: Oct 2000
Location: Tampa, FL
Java Question - do while loop problem/help

I have a question about do-while loops using Java and would like to tap some of the resident coders for assistance. Just a nudge in the right direction would be helpful, it's for my classwork I do after work.

I will try to be as articulate as I can. Thanks for any help!

Before the code you see starts, there is more code that I input batting stats that allows me to calculate things like batting averages and slugging percentage. Basically, I am making a calculator, I left out all the other stuff, because I think this is all that is important to the problem I am having.

Anyway, instead of re-running the program and re-inputting the batting stats, I want the program to loop back to the menu or exit. So I wanted my "do {" loop to start just before "//get menu choice". I chose to make a switch/case menu which seemed perfect for this.

The problem seems to be that the "do while" loop does not recognize the variable "formulas" that is created in the switch menu.

If I keep the start of the do-while loop where it is (inside the switch menu after int formulas is created), it works like a champ--read: if I choose to exit (formulas=6) it exits, and if I choose the other menu choices it infinately loops (as expected).

If anybody has any clues as to how to get my do-while loop to work, I'd appreciate it.

Code:
import java.util.Scanner; import javax.swing.*; public class Baseball { public static void main(String[] args) { //do { (commented out because it fails to compile) //get menu choice System.out.print("Please choose a formula from the menu below.\n"); System.out.print("1. Batting Average\n"); System.out.print("2. On Base Percentage\n"); System.out.print("3. Slugging Percentage\n"); System.out.print("4. Extra Base Hits\n"); System.out.print("5. Total Bases\n"); System.out.print("6. Exit Program\n"); System.out.print("Enter Your Choice Now: "); int formulas = stdin.nextInt(); do { (//not where I want it, but where I have to put it so it compiles) switch (formulas) { case 1: System.out.println("\nBatting Average"); System.out.println("The Batters Batting Average is " + battingAverage + "."); break; case 2: System.out.println("\nOn Base Percentage"); System.out.println("The Batters On Base Percentage is " + onBasePercentage + "."); break; case 3: System.out.println("\nSlugging Percentage"); System.out.println("The Batters Slugging Percentage is " + sluggingPercentage + "."); break; case 4: System.out.println("\nExtra Base Hits"); System.out.println("The Batters Extra Base Hits is " + extraBaseHits + "."); break; case 5: System.out.println("\nTotal Bases"); System.out.println("The Batters Total Bases is " + totalBases + "."); break; case 6: System.out.println("\nGoodbye"); break; } //closes switch menu } while (formulas != 6); } //close main } //closes class baseball

If I start the do-while loop at the commented out starting point before the menu, I get this error.

Code:
Baseball.java:117: cannot find symbol symbol : variable formulas location: class Baseball } while (formulas != 6); ^ 1 error


Last edited by Dutch : 08-12-2007 at 06:06 AM.
Dutch is offline   Reply With Quote
Old 08-12-2007, 07:02 AM   #2
Yossarian
High School Varsity
 
Join Date: Jul 2003
It's a scoping issue (as you knew I think).

In the scenario where it isn't compiling, you're creating 'formulas' inside the loop.

The "while (formulas != 6)" line is technically outside the loop and therefore can't see the formulas var because it is destroyed at the end of the loop.

To do what you want, you need to make sure formulas is created outwith the loop.

so...

change this line
Code:
int formulas = stdin.nextInt();
to this
Code:
formulas = stdin.nextInt();

and decalre formulas above the loop:
Code:
int formulas = 0;
Yossarian is offline   Reply With Quote
Old 08-12-2007, 08:55 AM   #3
Dutch
"Dutch"
 
Join Date: Oct 2000
Location: Tampa, FL
Wow, that was exactly it. When I tried that before I had...

Code:
int formulas = 0; do { ... int formulas = stdin.nextInt();

That redeclaration caused this

Code:
Baseball.java:88: formulas is already defined in main(java.lang.String[]) int formulas = stdin.nextInt(); ^ 1 error

I just didn't wrap my mind around what was wrong and I struggled with this for quite a while.

Thanks for the assistance!
Dutch is offline   Reply With Quote
Reply


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

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is On
Forum Jump


All times are GMT -5. The time now is 12:35 AM.



Powered by vBulletin Version 3.6.0
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.