| | |
Problem with loop, infinite or no loop at all
Thread Solved |
•
•
Join Date: Oct 2006
Posts: 178
Reputation:
Solved Threads: 2
Hi All
Im working on an exercise from a book im learning java with.
It is a console app, i have to make a menu 1-4 and then evaluate what number the user entered, carry out the command and then return to the menu.
My problem is that i tried to put the menu in a loop and it wouldnt work so i placed the if & else if statements in a loop and that works but i want the menu to reappear once the command has completed.
Here is the code for the reactor tester class
Here is the code for the Reactor class
I am not very good with loops so i keep messing it up.
Any help would be greatly appreciated
Many thanks
HLA91
Im working on an exercise from a book im learning java with.
It is a console app, i have to make a menu 1-4 and then evaluate what number the user entered, carry out the command and then return to the menu.
My problem is that i tried to put the menu in a loop and it wouldnt work so i placed the if & else if statements in a loop and that works but i want the menu to reappear once the command has completed.
Here is the code for the reactor tester class
Java Syntax (Toggle Plain Text)
import java.io.*; import java.util.*; class ReactorTester { private static BufferedReader buff; private static StringTokenizer st; private static String s; private static char[] ca; private static char c; // define main method public static void main(String[] args) throws IOException { //buff = new BufferedReader(new InputStreamReader( System.in )); InputStreamReader isr = new InputStreamReader( System.in ); BufferedReader stdin = new BufferedReader( isr ); char reply; Reactor b = new Reactor (); //generate object to test // increase, decrease, display, help, quit System.out.println("****************************************"); System.out.println("[1] Increase Temperature"); System.out.println("[2] Decrease Temperature"); System.out.println("[3] Display Current Temperature"); System.out.println("[4] HELP"); System.out.println("[5] Quit"); // get user input String input = stdin.readLine(); // convert string into an int value int choice = Integer.parseInt( input ); for (int i = 0;choice != 4;i++) { //use switch statement to determine what to do if (choice == 1) { boolean error = b.increaseTemp(); if (error) //check if increase raised an error { System.out.println("WARNING: TEMPERATURE EXCEEDING SAFE: ALARM RAISED!!!!"); } else { System.out.println("Temperature after increase is "); System.out.println(b.getTemperature()); } } else if (choice == 2) { boolean colderror = b.decreaseTemp(); if (colderror) // check if decrease caused error { System.out.println("WARNING: Temp at minimum, cannot decrease!!!!"); } else { System.out.println("Temperature after decrease is "); System.out.println(b.getTemperature()); } } else if (choice == 3) { System.out.println("Coming soon"); } } } }
Here is the code for the Reactor class
Java Syntax (Toggle Plain Text)
// controls reactor temperature ensuring it doesnt go over some maximum class Reactor { private static final int MAX=10; private int temperature; public Reactor() { temperature = 0; // set initial level } public int getTemperature () { // return current level return temperature; } public boolean decreaseTemp() { // decrease temp if temp > 0 else leave at 0 and produce error warning boolean coldAlarm; if (temperature > 0) { temperature --; coldAlarm = false; } else { coldAlarm = true; } return coldAlarm; } public boolean increaseTemp() { // increase temperature if safe, drop to zero and raise alarm if not boolean hotAlarm; if (temperature < MAX) { temperature ++; hotAlarm = false; } else { temperature =0; hotAlarm = true; } return hotAlarm; } }
I am not very good with loops so i keep messing it up.
Any help would be greatly appreciated
Many thanks
HLA91
You know your a geek, if you introduce your wife as "mylady@home.wife"
•
•
Join Date: Jan 2008
Posts: 3,955
Reputation:
Solved Threads: 515
JAVA Syntax (Toggle Plain Text)
// increase, decrease, display, help, quit System.out.println("****************************************"); System.out.println("[1] Increase Temperature"); System.out.println("[2] Decrease Temperature"); System.out.println("[3] Display Current Temperature"); System.out.println("[4] HELP"); System.out.println("[5] Quit"); // get user input String input = stdin.readLine(); // convert string into an int value int choice = Integer.parseInt( input ); for (int i = 0;choice != 4;i++) { //use switch statement to determine what to do if (choice == 1) { boolean error = b.increaseTemp(); if (error) //check if increase raised an error { System.out.println("WARNING: TEMPERATURE EXCEEDING SAFE: ALARM RAISED!!!!"); } else { System.out.println("Temperature after increase is "); System.out.println(b.getTemperature()); } } else if (choice == 2) { boolean colderror = b.decreaseTemp(); if (colderror) // check if decrease caused error { System.out.println("WARNING: Temp at minimum, cannot decrease!!!!"); } else { System.out.println("Temperature after decrease is "); System.out.println(b.getTemperature()); } } else if (choice == 3) { System.out.println("Coming soon"); } }
You don't use the variable i anywhere, so I would change the for loop to a while loop or a do-while loop. You want to do it at least once, so make it a do-while:
Java Syntax (Toggle Plain Text)
do { // code } while (choice != 5);
If you want lines 1 to 13 (code that displays menu and accepts input) to repeat, simply put that code INSIDE the do-while loop at the top of it and it will display the options each time through the loop. You are going to have to elaborate on how putting the menu inside of the loop did not work, because it seems to me that that is exactly what you want to do and that it should work. Put anything you want to repeat inside the loop and anything you don't outside the loop.
•
•
Join Date: Oct 2006
Posts: 178
Reputation:
Solved Threads: 2
Thanks for the quick reply VernonDozier
I placed the menu within a do while loop but the problem now is that the choice is within the loop so it wont compile, but getting the choice input has to be after the menu is displayed and i need the menu in the loop so now i have another problem which i dont know how to fix
and the error is
Thanks
HLA91
I placed the menu within a do while loop but the problem now is that the choice is within the loop so it wont compile, but getting the choice input has to be after the menu is displayed and i need the menu in the loop so now i have another problem which i dont know how to fix
java Syntax (Toggle Plain Text)
// define main method public static void main(String[] args) throws IOException { //buff = new BufferedReader(new InputStreamReader( System.in )); InputStreamReader isr = new InputStreamReader( System.in ); BufferedReader stdin = new BufferedReader( isr ); char reply; Reactor b = new Reactor (); //generate object to test do { // increase, decrease, display, help, quit System.out.println("****************************************"); System.out.println("[1] Increase Temperature"); System.out.println("[2] Decrease Temperature"); System.out.println("[3] Display Current Temperature"); System.out.println("[4] HELP"); System.out.println("[5] Quit"); // get user input String input = stdin.readLine(); // convert string into an int value int choice = Integer.parseInt( input ); for (int i = 0;choice != 4;i++) //use switch statement to determine what to do if (choice == 1) { boolean error = b.increaseTemp(); if (error) //check if increase raised an error { System.out.println("WARNING: TEMPERATURE EXCEEDING SAFE: ALARM RAISED!!!!"); } else { System.out.println("Temperature after increase is "); System.out.println(b.getTemperature()); } } else if (choice == 2) { boolean colderror = b.decreaseTemp(); if (colderror) // check if decrease caused error { System.out.println("WARNING: Temp at minimum, cannot decrease!!!!"); } else { System.out.println("Temperature after decrease is "); System.out.println(b.getTemperature()); } } else if (choice == 3) { System.out.println("Coming soon"); } }while (choice != 5);
and the error is
•
•
•
•
Reactor Tester.java:77: cannot resolve symbol
symbol : variable choice
location: class ReactorTester
}while (choice != 5);
1 error
HLA91
Last edited by HLA91; Jul 29th, 2008 at 1:26 pm.
You know your a geek, if you introduce your wife as "mylady@home.wife"
•
•
Join Date: Jan 2008
Posts: 3,955
Reputation:
Solved Threads: 515
When you add the do-while loop, get rid of the for-loop. If your choice variable goes out of scope by the time you hit this statement:
define (and initialize to some number, doesn't matter what, but use 0) the variable choice ABOVE the do-while loop. This guarantees that it won't go out of scope:
When you use choice inside the loop, leave off the
rather than
inside the while loop. Again, get rid of the for-loop altogether. Keep the if-statements that are inside of it, but you don't want the for-loop. Your loop is the do-while loop.
Java Syntax (Toggle Plain Text)
while (choice != 5);
define (and initialize to some number, doesn't matter what, but use 0) the variable choice ABOVE the do-while loop. This guarantees that it won't go out of scope:
JAVA Syntax (Toggle Plain Text)
int choice = 0; do { // display menu // read user input into variable choice // if-statements code } while (choice != 5);
When you use choice inside the loop, leave off the
int since that is taken care of above the do-while loop. For example, use this: Java Syntax (Toggle Plain Text)
choice = Integer.parseInt( input );
rather than
Java Syntax (Toggle Plain Text)
int choice = Integer.parseInt( input );
inside the while loop. Again, get rid of the for-loop altogether. Keep the if-statements that are inside of it, but you don't want the for-loop. Your loop is the do-while loop.
![]() |
Similar Threads
- Is Halting Problem Valid for P(the class of all Pascal programs )? (Pascal and Delphi)
- Strange cout problem (C++)
- XP Startup Problem: Infinite Loop (Windows NT / 2000 / XP)
- Strange Excel infinite-loop problem (Windows Software)
- Roulette program simulation problem (Python)
- Help Me Solve My Infinite Loop (C++)
- infinite loop... (C++)
- C pointer problem (C)
- Can Someone Tell me why my program does not run (C++)
- problem with Chars and Strings (C++)
Other Threads in the Java Forum
- Previous Thread: Liferay and Alfresco integration
- Next Thread: About How to run JavaMail programs??
Views: 719 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for Java
actionlistener android applet arguments array arraylist arrays binary bluetooth c++ chat class classes client code compiler component convert converter coordinates data database db design detection draw eclipse error event exception file forloop fractal givemetehcodez graphics gridlayout gui helpwithhomework homeworkassignment html ide image images input integer j2me java jframe jmf jni jpanel jtextfield key lazy linked linked-list list loop main method methods mobile netbeans newbie node number object oracle os parameter pattern phone pixel printing problem program programming read remote remove return robot scanner server set size sms sort sql string swing system test text text-file thread transfer tree user web






