943,884 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 831
  • Java RSS
Jul 29th, 2008
0

Problem with loop, infinite or no loop at all

Expand Post »
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
Java Syntax (Toggle Plain Text)
  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

Java Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 7
Solved Threads: 2
Junior Poster
HLA91 is offline Offline
177 posts
since Oct 2006
Jul 29th, 2008
0

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

JAVA Syntax (Toggle Plain Text)
  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:

Java Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Jul 29th, 2008
0

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

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

java Syntax (Toggle Plain Text)
  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

Quote ...
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 2:26 pm.
Reputation Points: 7
Solved Threads: 2
Junior Poster
HLA91 is offline Offline
177 posts
since Oct 2006
Jul 29th, 2008
1

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

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:

Java Syntax (Toggle Plain Text)
  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:

JAVA Syntax (Toggle Plain Text)
  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:

Java Syntax (Toggle Plain Text)
  1. choice = Integer.parseInt( input );

rather than

Java Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Jul 29th, 2008
0

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

All works now
Thankyou for your time VernonDozier, most appreciated
Reputation Points: 7
Solved Threads: 2
Junior Poster
HLA91 is offline Offline
177 posts
since Oct 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Liferay and Alfresco integration
Next Thread in Java Forum Timeline: About How to run JavaMail programs??





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC