Problem with loop, infinite or no loop at all

Thread Solved

Join Date: Oct 2006
Posts: 178
Reputation: HLA91 is an unknown quantity at this point 
Solved Threads: 2
HLA91 HLA91 is offline Offline
Junior Poster

Problem with loop, infinite or no loop at all

 
0
  #1
Jul 29th, 2008
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
  1.  
  2. import java.io.*;
  3. import java.util.*;
  4.  
  5. class ReactorTester
  6. {
  7. private static BufferedReader buff;
  8. private static StringTokenizer st;
  9. private static String s;
  10. private static char[] ca;
  11. private static char c;
  12.  
  13. // define main method
  14. public static void main(String[] args) throws IOException
  15. {
  16.  
  17. //buff = new BufferedReader(new InputStreamReader( System.in ));
  18. InputStreamReader isr = new InputStreamReader( System.in );
  19. BufferedReader stdin = new BufferedReader( isr );
  20. char reply;
  21. Reactor b = new Reactor (); //generate object to test
  22.  
  23.  
  24.  
  25. // increase, decrease, display, help, quit
  26. System.out.println("****************************************");
  27. System.out.println("[1] Increase Temperature");
  28. System.out.println("[2] Decrease Temperature");
  29. System.out.println("[3] Display Current Temperature");
  30. System.out.println("[4] HELP");
  31. System.out.println("[5] Quit");
  32.  
  33. // get user input
  34. String input = stdin.readLine();
  35.  
  36. // convert string into an int value
  37. int choice = Integer.parseInt( input );
  38.  
  39.  
  40. for (int i = 0;choice != 4;i++)
  41. {
  42.  
  43. //use switch statement to determine what to do
  44. if (choice == 1)
  45. {
  46. boolean error = b.increaseTemp();
  47. if (error) //check if increase raised an error
  48. {
  49. System.out.println("WARNING: TEMPERATURE EXCEEDING SAFE: ALARM RAISED!!!!");
  50. }
  51. else
  52. {
  53.  
  54. System.out.println("Temperature after increase is ");
  55. System.out.println(b.getTemperature());
  56. }
  57. }
  58.  
  59. else if (choice == 2)
  60. {
  61. boolean colderror = b.decreaseTemp();
  62. if (colderror) // check if decrease caused error
  63. {
  64. System.out.println("WARNING: Temp at minimum, cannot decrease!!!!");
  65. }
  66. else
  67. {
  68. System.out.println("Temperature after decrease is ");
  69. System.out.println(b.getTemperature());
  70. }
  71. }
  72. else if (choice == 3)
  73. {
  74. System.out.println("Coming soon");
  75. }
  76. }
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85. }
  86. }

Here is the code for the Reactor class

  1. // controls reactor temperature ensuring it doesnt go over some maximum
  2. class Reactor
  3. {
  4. private static final int MAX=10;
  5. private int temperature;
  6.  
  7. public Reactor()
  8. {
  9. temperature = 0; // set initial level
  10. }
  11.  
  12. public int getTemperature ()
  13. {
  14. // return current level
  15. return temperature;
  16. }
  17.  
  18. public boolean decreaseTemp()
  19. {
  20. // decrease temp if temp > 0 else leave at 0 and produce error warning
  21. boolean coldAlarm;
  22. if (temperature > 0)
  23. {
  24. temperature --;
  25. coldAlarm = false;
  26. }
  27. else
  28. {
  29. coldAlarm = true;
  30. }
  31. return coldAlarm;
  32. }
  33.  
  34.  
  35. public boolean increaseTemp()
  36. {
  37. // increase temperature if safe, drop to zero and raise alarm if not
  38. boolean hotAlarm;
  39. if (temperature < MAX)
  40. {
  41. temperature ++;
  42. hotAlarm = false;
  43. }
  44. else
  45. {
  46. temperature =0;
  47. hotAlarm = true;
  48. }
  49. return hotAlarm;
  50. }
  51. }

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"
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,955
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 515
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Problem with loop, infinite or no loop at all

 
0
  #2
Jul 29th, 2008
  1. // increase, decrease, display, help, quit
  2. System.out.println("****************************************");
  3. System.out.println("[1] Increase Temperature");
  4. System.out.println("[2] Decrease Temperature");
  5. System.out.println("[3] Display Current Temperature");
  6. System.out.println("[4] HELP");
  7. System.out.println("[5] Quit");
  8.  
  9. // get user input
  10. String input = stdin.readLine();
  11.  
  12. // convert string into an int value
  13. int choice = Integer.parseInt( input );
  14.  
  15.  
  16. for (int i = 0;choice != 4;i++)
  17. {
  18.  
  19. //use switch statement to determine what to do
  20. if (choice == 1)
  21. {
  22. boolean error = b.increaseTemp();
  23. if (error) //check if increase raised an error
  24. {
  25. System.out.println("WARNING: TEMPERATURE EXCEEDING SAFE: ALARM RAISED!!!!");
  26. }
  27. else
  28. {
  29.  
  30. System.out.println("Temperature after increase is ");
  31. System.out.println(b.getTemperature());
  32. }
  33. }
  34.  
  35. else if (choice == 2)
  36. {
  37. boolean colderror = b.decreaseTemp();
  38. if (colderror) // check if decrease caused error
  39. {
  40. System.out.println("WARNING: Temp at minimum, cannot decrease!!!!");
  41. }
  42. else
  43. {
  44. System.out.println("Temperature after decrease is ");
  45. System.out.println(b.getTemperature());
  46. }
  47. }
  48. else if (choice == 3)
  49. {
  50. System.out.println("Coming soon");
  51. }
  52. }

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:

  1. do
  2. {
  3. // code
  4. }
  5. 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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 178
Reputation: HLA91 is an unknown quantity at this point 
Solved Threads: 2
HLA91 HLA91 is offline Offline
Junior Poster

Re: Problem with loop, infinite or no loop at all

 
0
  #3
Jul 29th, 2008
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

  1. // define main method
  2. public static void main(String[] args) throws IOException
  3. {
  4.  
  5. //buff = new BufferedReader(new InputStreamReader( System.in ));
  6. InputStreamReader isr = new InputStreamReader( System.in );
  7. BufferedReader stdin = new BufferedReader( isr );
  8. char reply;
  9. Reactor b = new Reactor (); //generate object to test
  10.  
  11.  
  12. do
  13. {
  14.  
  15. // increase, decrease, display, help, quit
  16. System.out.println("****************************************");
  17. System.out.println("[1] Increase Temperature");
  18. System.out.println("[2] Decrease Temperature");
  19. System.out.println("[3] Display Current Temperature");
  20. System.out.println("[4] HELP");
  21. System.out.println("[5] Quit");
  22.  
  23. // get user input
  24. String input = stdin.readLine();
  25.  
  26. // convert string into an int value
  27. int choice = Integer.parseInt( input );
  28.  
  29.  
  30. for (int i = 0;choice != 4;i++)
  31.  
  32.  
  33. //use switch statement to determine what to do
  34. if (choice == 1)
  35. {
  36. boolean error = b.increaseTemp();
  37. if (error) //check if increase raised an error
  38. {
  39. System.out.println("WARNING: TEMPERATURE EXCEEDING SAFE: ALARM RAISED!!!!");
  40. }
  41. else
  42. {
  43.  
  44. System.out.println("Temperature after increase is ");
  45. System.out.println(b.getTemperature());
  46. }
  47. }
  48.  
  49. else if (choice == 2)
  50. {
  51. boolean colderror = b.decreaseTemp();
  52. if (colderror) // check if decrease caused error
  53. {
  54. System.out.println("WARNING: Temp at minimum, cannot decrease!!!!");
  55. }
  56. else
  57. {
  58. System.out.println("Temperature after decrease is ");
  59. System.out.println(b.getTemperature());
  60. }
  61. }
  62. else if (choice == 3)
  63. {
  64. System.out.println("Coming soon");
  65. }
  66. }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
Thanks

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"
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,955
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 515
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Problem with loop, infinite or no loop at all

 
1
  #4
Jul 29th, 2008
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:

  1. 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:

  1. int choice = 0;
  2.  
  3. do
  4. {
  5. // display menu
  6. // read user input into variable choice
  7.  
  8. // if-statements code
  9. }
  10. 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:

  1. choice = Integer.parseInt( input );

rather than

  1. 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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 178
Reputation: HLA91 is an unknown quantity at this point 
Solved Threads: 2
HLA91 HLA91 is offline Offline
Junior Poster

Re: Problem with loop, infinite or no loop at all

 
0
  #5
Jul 29th, 2008
All works now
Thankyou for your time VernonDozier, most appreciated
You know your a geek, if you introduce your wife as "mylady@home.wife"
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 719 | Replies: 4
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC