It's not a secret anymore!
The moose likes Beginning Java and the fly likes trouble with loops and invoking a method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "trouble with loops and invoking a method" Watch "trouble with loops and invoking a method" New topic

trouble with loops and invoking a method

Christine Marie

Joined: Apr 17, 2010
Posts: 5
Many thanks for the help with corrections on the basic version of this lab.

What I have is a basic math game - it asks for your name and choice of operation, spits out random numbers, and compares your answer to the actual answer and gives a result. Then, it ends. Now, my teacher wants us to add an option for the player to continue going until they type some option for "quit".

By itself, I was able to add a System.exit that ran properly and ended the game. However, the best way I could figure to continue was to offer another choice point- "would you like to continue? press 6. If you would like to quit, press 7". I tried writing a loop that either invokes the run method or quits, but I keep getting an illegal start of expression error. (Way way at the bottom.)

Am I barking up the wrong tree with trying to invoke this method again? Suggestions?

All help is appreciated!

marc weber

Joined: Aug 31, 2004
Posts: 11343

Just took a quick glance, but that semicolon in line 213 looks troublesome.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Christine Marie

Joined: Apr 17, 2010
Posts: 5
Agreed, thanks =) It got stuck there when I was jockeying some things about... but I took it out, and did a test print on both of those loops. They both run, so I know my loop is valid, I'm just at a loss as to how to create this infinite loop scenario.
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

Take a look at the Java api for the Integer Class; specifically looking at the parseInt method. Look at what the method does, does it convert an integer to a string? or a string to an integer? then take a look at your program and the if then conditions that evaluate your choice variable.

Java Integer Class:

As far as the infinite loop goes you could use a boolean value and a while loop;

Hope this helps,

"If the facts don't fit the theory, get new facts" --Albert Einstein
Manjunatha Kampli Kottal

Joined: Apr 20, 2010
Posts: 6
The ideal way of coding a menu driven program is to use a infinite loop either with while loop or with for loop until the execution stops along with a switch control. for instance

InputReader choice= new InputReader();
int operation= Integer.parseInt(choice);
case 1:System.out.println("Do something here");
case 2:System.out.println("Do something here");
case 6:System.out.println("Bye Bye");
default:System.out.println("Invalid Choice.Please enter the correct Choice.");



Its not rule but this is how its practiced by the programmers as it simplifies your debugging process
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 1141

Manju Arasula, welcome to Coderanch.
use code tags next time to post any code.

My personal choice in such situation would be a do-while loop since you want the loop to run at least once.
@ the next of the loop, the user is prompted to continue, if he chooses yes, the loop continues again...

but thats just me.(I am weird at times )

I agree. Here's the better link:

I agree. Here's the link:
subject: trouble with loops and invoking a method
It's not a secret anymore!