File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "trouble with loops and invoking a method" Watch "trouble with loops and invoking a method" New topic
Author

trouble with loops and invoking a method

Christine Marie
Greenhorn

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
Sheriff

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
sscce.org
Christine Marie
Greenhorn

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:
http://java.sun.com/javase/6/docs/api/java/lang/Integer.html

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

Hope this helps,
Hunter


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

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
for(;;)
{

InputReader choice= new InputReader();
int operation= Integer.parseInt(choice);
switch(operation)
{
case 1:System.out.println("Do something here");
break;
case 2:System.out.println("Do something here");
break;
.......................
........................
........................
case 6:System.out.println("Bye Bye");
System.exit(0);
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: 917

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 )


My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: trouble with loops and invoking a method
 
Similar Threads
loops and boolean operators
what's wrong with this code?
help needed in makin GUI (in netbeans) for a maths program(with lots of loops).
Methods
coin flip assignment help