Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: Do while help

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Do while help

    public void mainmenu ()
        {
            title ();
            System.out.print("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.print ("That's not an integer. Please Try Again");
                }
            }
            while (proc != 5 || proc != 2 || proc != 3);
        }
    It doesn't loop if I input a number other than 5,2 or 3. Help? Thanks in advance.

  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Do while help

    Why do you break the while loop?

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Do while help

    cause it keeps on going if I don't.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Do while help

    Check your loop condition. Remember only 1 condition need be true in or statements.
    Improving the world one idiot at a time!

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Do while help

    Yeah it still doesn't work though I changed it to this.

    if (proc == 5 || proc ==3 || proc == 2)
                        runLoop = false;
    runLoop is declared as a Boolean and true.

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Do while help

    Post your new code. And provide more details. "it doesn't work" gives zero information.
    Improving the world one idiot at a time!

  7. #7
    Junior Member
    Join Date
    Nov 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Do while help

    // 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;
        static double none = 0;
        static char unit2;
        static char amount2;
        static boolean runLoop = true;
     
        private void title ()
        {
            c.print (' ', 31);
            c.print ("Liquid Conversion");
            c.println ();
        }
     
     
        public void intro ()
        {
            c = new Console ();
            title ();
            c.println ("This program will allow you to conver an amount of litre into kilolitres, millilitres, barrels, cups, teaspoons, tablespoons, gallons, quarts, and pints. Press 0 to go back to the main menu.");
     
            while (runLoop = true)
            {
                try
                {
                    procstr = c.readLine ();
                    proc = Integer.parseInt (procstr);
                    if (proc == 0)
                        mainmenu();
                    if (proc != 0)
                        runLoop = true;
                    break;
                }
                catch (NumberFormatException e)
                {
                    c.println ("That's not an integer. Please Try Again");
                }
            }
        }
     
     
        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:");
     
            while (runLoop = true)
            {
                try
                {
                    procstr = c.readLine ();
                    proc = Integer.parseInt (procstr);
                    if (proc == 3)
                        askData ();
                    if (proc == 2)
                        intro ();
                    if (proc == 5)
                        goodbye ();
                    if (proc == 5 || proc == 3 || proc == 2)
                        runLoop = false;
                    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 ("The amount your converting will be in litres. 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 unit you want your amount to be converted to: ");
     
            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");
                }
            }
     
        }
     
     
        private double convertLiquid (double none2, char hunt2, char new2)
        {
            if (unit == 1)
                return (amount * 33.8140227);
            if (unit == 2)
                return (amount * 0.001);
            if (unit == 3)
                return (amount * 1000);
            if (unit == 4)
                return (amount * 0.00852167911);
            if (unit == 5)
                return (amount * 4.22675284);
            if (unit == 6)
                return (amount * 202.884136);
            if (unit == 7)
                return (amount * 67.6280454);
            if (unit == 8)
                return (amount * 0.264172052);
            if (unit == 9)
                return (amount * 1.05668821);
            if (unit == 10)
                return (amount * 2.11337642);
            else
                return (none);
        }
     
     
        public void displayData ()
        {
            c = new Console ();
            title ();
            c.println ("Your amount is " + amount + " converted to " + convertLiquid (none2,hunt2,new2));
            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
    That's the entire code. It doesn't loop if I put another number other than 5,2,3. I want it to keep looping until the user input one of the three numbers (5,3, or 2)
    Last edited by Daigan; November 16th, 2011 at 10:50 PM.

  8. #8
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Do while help

    Once again check your loop condition.
    while (runLoop = true)
    Improving the world one idiot at a time!

  9. #9
    Junior Member
    Join Date
    Nov 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Do while help

    I removed the true in the while statement. And changed
    if (proc != 0)
    runLoop = true;
    that from true to false and it still doesn't loop. Doesn't it mean that while runLoop is true don't stop looping?

  10. #10
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Do while help

    Single = is assignment not comparison. Removing the = true is the only change you need to make. Other changes you made have introduced some other bug which I cannot see as you have not posted your latest version of the code.
    Improving the world one idiot at a time!

  11. #11
    Junior Member
    Join Date
    Nov 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Do while help

    // 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;
        static boolean runLoop = true;
     
        private void title ()
        {
            c.print (' ', 31);
            c.print ("Liquid Conversion");
            c.println ();
        }
     
     
        public void intro ()
        {
            c = new Console ();
            title ();
            c.println ("This program will allow you to conver an amount of litre into kilolitres, millilitres, barrels, cups, teaspoons, tablespoons, gallons, quarts, and pints. Press 0 to go back to the main menu.");
     
            while (runLoop)
            {
                try
                {
                    procstr = c.readLine ();
                    proc = Integer.parseInt (procstr);
                    if (proc == 0)
                        mainmenu();
                    if (proc != 0)
                        runLoop = true;
                    break;
                }
                catch (NumberFormatException e)
                {
                    c.println ("That's not an integer. Please Try Again");
                }
            }
        }
     
     
        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:");
     
            while (runLoop)
            {
                try
                {
                    procstr = c.readLine ();
                    proc = Integer.parseInt (procstr);
                    if (proc == 3)
                        askData ();
                    if (proc == 2)
                        intro ();
                    if (proc == 5)
                        goodbye ();
                    if (proc == 5 || proc == 3 || proc == 2)
                        runLoop = false;
                    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 ("The amount your converting will be in litres. 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 unit you want your amount to be converted to: ");
     
            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");
                }
            }
     
        }
     
     
        private double convertLiquid (double none2, char unit2, char amount2)
        {
            if (unit == 1)
                return (amount * 33.8140227);
            if (unit == 2)
                return (amount * 0.001);
            if (unit == 3)
                return (amount * 1000);
            if (unit == 4)
                return (amount * 0.00852167911);
            if (unit == 5)
                return (amount * 4.22675284);
            if (unit == 6)
                return (amount * 202.884136);
            if (unit == 7)
                return (amount * 67.6280454);
            if (unit == 8)
                return (amount * 0.264172052);
            if (unit == 9)
                return (amount * 1.05668821);
            if (unit == 10)
                return (amount * 2.11337642);
            else
                return (none2 = 0);
        }
     
     
        public void displayData ()
        {
            c = new Console ();
            title ();
            c.println ("Your amount is " + amount + " converted to " + convertLiquid (none2, unit2, amount2));
            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

    That's what I have so far. I still get errors on the parameters of my convertLiquid thought. And I also removed the = true and it still doesn't loop the way I want it to.

  12. #12
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Do while help

    You still are not providing details. Once again "it doesn't work" provides zero information. If you get errors then post them. If you get incorrect behaviour then describe what happens and what you expect to happen.
    Improving the world one idiot at a time!

  13. #13
    Junior Member
    Join Date
    Nov 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Do while help

    I'm expecting the loop in the intro method to keep looping if the user inputs a number other than 0. Same goes for the mainmenu method, I expect it to keep asking an input until it gets either a 5,3, or 2.

  14. #14
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Do while help

    After a brief inspection of your code there a plenty of logical problems throughout your code.

    Why are all the variables static?
    Why do you keep creating a new Console object all over the place?
    When you are uin the mainmenu method and user enter 2 for the intro, once this method finishes it will automatically return to the mainmenu method. This means that the entire loop in the intro method is pointless.

    Step away from the computer, grab some paper and a pencil. Start making notes of what the program should do and in what order. Make some diagrams and flow charts. All this will help you get a better idea of how the program should fit together. Once you have done that writing the code should become easier. Right now it seems to me that you are running about like a chook without a head making wild changes without really understanding what you are doing.
    Improving the world one idiot at a time!

  15. #15
    Junior Member
    Join Date
    Nov 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Do while help

    Alright, thanks for your advice.

  16. #16
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Do while help

    I just noticed you still have the break statements which you were told about waaay back in reply 1.
    Improving the world one idiot at a time!