public void mainmenu ()
    {
        title ();
       System.out.println ("If you wish to exit press 5, if you want to go the intro press 2, and if you want to continue 3:");

        do
        {
            try
            {
                procstr = c.readLine ();
                proc = Integer.parseInt (procstr);
                if (proc == 3)
                    askData ();
                if (proc == 2)
                    intro ();
                if (proc == 5)
                    goodbye ();
                break;
            }
            catch (NumberFormatException e)
            {
                System.out.println ("That's not an integer. Please Try Again");
            }
        }
        while (proc != 5 || proc != 2 || proc != 3);
    }

That's what I got so far but it doesn't loop even if I input a number that isn't 5,2,3.

Recommended Answers

All 11 Replies

but it doesn't loop even if

Can you show the console from when you execute your code that shows what you input and how the code executed?

Can you show the console from when you execute your code that shows what you input and how the code executed?

public static void main (String[] args)
    {
        c = new Console ();
        LiquidConversion d;
        d = new LiquidConversion ();
        d.mainmenu ();
    }

That one?

By console I meant the command prompt window where you enter the java command to execute your program and where the program prints messages and you enter responses.
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

I'm using Ready to Program Java and it doesn't have those options on the console window so I just copied the output

Liquid Conversion
If you wish to exit press 5, if you want to go the intro press 2, and if you want to continue 3:

- Then after the : that's where you put the input

I think you wanted this one my bad.

// The "LiquidConversion" class.
import java.awt.*;
import hsa.Console;

public class LiquidConversion
{
    static Console c;           // The output console
    static String procstr;
    static int proc;
    static String amountstr;
    static double amount;
    static String unitstr;
    static int unit;

    private void title ()
    {
        c.print (' ', 31);
        c.print ("Liquid Conversion");
        c.println ();
    }


    public void intro ()
    {
    }


    public void mainmenu ()
    {
        title ();
        c.println ("If you wish to exit press 5, if you want to go the intro press 2, and if you want to continue 3:");

        do
        {
            try
            {
                procstr = c.readLine ();
                proc = Integer.parseInt (procstr);
                if (proc == 3)
                    askData ();
                if (proc == 2)
                    intro ();
                if (proc == 5)
                    goodbye ();
                break;
            }
            catch (NumberFormatException e)
            {
                c.println ("That's not an integer. Please Try Again");
            }
        }
        while (proc != 5 || proc != 2 || proc != 3);
    }


    public void askData ()
    {
        c = new Console ();
        c.print ("1 for Litres, 2 for Kilolitres, 3 for Millilitres, 4 for Barrels, 5 for cups, 6 for teaspoons, 7 for tablespoons, 8 gallons, 9 for quartz and 10 for pints.");
        c.println ("Please input the amount you want to convert");

        while (true)

            {
                try
                {
                    amountstr = c.readLine ();
                    amount = Double.parseDouble (amountstr);
                    break;
                }
                catch (NumberFormatException e)
                {
                    c.println ("That's not an integer. Please Try Again");
                }
            }

        c.println ("Please input the amount's unit your converting");

        while (true)
        {
            try
            {
                unitstr = c.readLine ();
                unit = Integer.parseInt (unitstr);
                displayData ();
                break;
            }
            catch (NumberFormatException e)
            {
                c.println ("That's not an integer. Please Try Again");
            }
        }

    }


    public void displayData ()
    {
        c = new Console ();
        title ();
        c.println (" ");
        c.println ("If you want to go back the main menu press 1, if you wish to exit press 0: ");
        while (true)

            {
                try
                {
                    procstr = c.readLine ();
                    proc = Integer.parseInt (procstr);
                    if (proc == 1)
                        mainmenu ();
                    if (proc == 0)
                        goodbye ();
                    break;
                }
                catch (NumberFormatException e)
                {
                    c.println ("That's not an integer. Please Try Again");
                }
            }


    }


    public void goodbye ()
    {
        c = new Console ();
        title ();
        c.print ("Thank you for using the program!");
    }


    public static void main (String[] args)
    {
        c = new Console ();
        LiquidConversion d;
        d = new LiquidConversion ();
        d.mainmenu ();

        // Place your program here.  'c' is the output console
    } // main method
} // LiquidConversion class

Once your not-looping-at-all issue is solved you'll have to do something about your terminating condition because your next problem is that it will never stop looping.

Your terminating condition will always be true: while (proc != 5 || proc != 2 || proc != 3); It doesn't matter what value proc has (5 for example), it won't be either of the other two values.

Isn't my terminating condition if proc doesn't equal to either 5, 2 or, 3 it will keep looping?

In plain words it says "while proc is not equal to 5 or proc not equal to 2 or proc not equal to 3". In order for the condition to be false proc would have to be equal to 5 AND equal to 2 AND equal to 3. Clearly not possible.

In relation to the main issue you're having, if you enter 3 does the program ask you the next question?

I changed it to while (proc == 5 || proc == 2 || proc == 3); and it still the same.

Yes it does, it goes to the askData method

What are the conditions where you want the program to loop?

Yes, the while problem was only going to show up once you've solved the problem of why it's not looping at all.

I'm assuming that the program just goes silent without any kind of error message at all. Tell me if I'm wrong on this.

You don't need new Console (); at the start of askData, displayData and goodbye. Take those out, leaving only the one in your main method. See what happens there.

If that doesn't help, try putting in some lines around the place like c.println ("at end of askData"); and c.println("back from askData"); to see exactly where the flow of control goes on the way out of your code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.