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

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

// 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

// 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:

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.

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

// 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

Thanks

HLA91

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:

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:

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:

choice = Integer.parseInt( input );

rather than

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.

Comments
Credit is given when credit is due =)

All works now
Thankyou for your time VernonDozier, most appreciated

This question has already been answered. Start a new discussion instead.