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 10 of 10

Thread: Please help with my while loop that turned into infinite loop!

  1. #1
    Junior Member Hazmat210's Avatar
    Join Date
    Feb 2012
    Location
    San Antonio,Tx
    Posts
    23
    My Mood
    Nerdy
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Post Please help with my while loop that turned into infinite loop!

    I wrote the code below for class assignment was to create program that does a calculation based on unput from menu and would continue to loop until user inputed exit number. But when compiled instead of prompting menu again it goes into an infinite loop i think. Please any help is appreciated. I believe the break is not working properly not sure if I am using it right.

     
     
    import java.util.Scanner;
     
    public class Calculator
        {
            public static void main(String[] args)
            {
     
     
            int choice, choice2, choice3;
            float result;
     
            Scanner scan = new Scanner (System.in);
     
            System.out.println("What would you like to do? ");
            System.out.println("1. Add");
            System.out.println("2. Subtract");
            System.out.println("3. Multiply");
            System.out.println("4. Divide");
            System.out.println("5. Exit");
            System.out.print("Selection: ");
            choice = scan.nextInt();
     
            if (choice == 5)
            {
                System.out.println("Maybe next time try a calculation.");
            }
            else
            {
            System.out.print("Enter first number: ");
            choice2 = scan.nextInt();
            System.out.print("Enter second number: ");
            choice3 = scan.nextInt();
     
           do
                {
                switch (choice)
                    {
                    case 1:
                    //Add 2 integers
     
                    System.out.println(choice2 + "+" + choice3 + "=" + (choice2 + choice3));
                    break;
     
                    case 2:
                    //Subtract 2 integers
     
                    System.out.println(choice2 + "-" + choice3 + "=" + (choice2 - choice3));
                    break;
     
                    case 3:
                    //Multiply 2 integers
     
                    System.out.println(choice2 + "*" + choice3 + "=" + (choice2 * choice3));
                    break;
     
                    case 4:
                    //Divide 2 integers
     
                    result = choice2 / choice3;
                    System.out.println(choice2 + "/" + choice3 + "=" + result);
                    break;
     
                    case 5:
                    //Exit
     
                    default:
     
                    //
                    break;
                    }
                }while (choice != 5);
     
            }
        }
    }
    Last edited by Hazmat210; March 10th, 2012 at 01:14 AM. Reason: add another statement for insight on my problem


  2. #2
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please help with my while loop that turned into infinite loop!

    in your code you don't need do-while loop there.. remove loop and try.. its working..

  3. #3
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Please help with my while loop that turned into infinite loop!

    Hello Hazmat210,
    the do...while loop is in the else block which means that choice doesn't equal to 5 and so the statement choice != 5 is always true and that's why your loop is infinite. For an assignment like yours a do...while loop is appropriate but i think you should work a little more with your logic.

  4. The Following User Says Thank You to andreas90 For This Useful Post:

    Hazmat210 (March 10th, 2012)

  5. #4
    Junior Member Hazmat210's Avatar
    Join Date
    Feb 2012
    Location
    San Antonio,Tx
    Posts
    23
    My Mood
    Nerdy
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Please help with my while loop that turned into infinite loop!

    Thank you for the replies the program should go like this:

    Display five options for a calculation
    Ask for selection
    First integer
    Second integer
    if 5 is selected program terminates.
    else perform calculation with integers
    Display equation and answer

    Then repeat selection menu again until 5 is chosen.

    The reason i put the first selection in the if else statement was so that if 5 was chosen then the program could terminate otherwise it prompts for the first and second integers.
    Last edited by Hazmat210; March 10th, 2012 at 04:03 PM.

  6. #5
    Junior Member Hazmat210's Avatar
    Join Date
    Feb 2012
    Location
    San Antonio,Tx
    Posts
    23
    My Mood
    Nerdy
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Please help with my while loop that turned into infinite loop!

    I initially wrote the code without the if else statement and the loop was still infinite it just keep displaying the equation until I closed the program. But I can see how the if else statement makes it infinite. It's the actual while loop that I am having trouble with it doesn't break or if it is it doesn't go back to the menu selection which is where i want it to go.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,110
    Thanks
    65
    Thanked 2,717 Times in 2,667 Posts

    Default Re: Please help with my while loop that turned into infinite loop!

    If you want the code to loop back to the menu part, you will have to put that inside of the loop.

  8. The Following User Says Thank You to Norm For This Useful Post:

    Hazmat210 (March 10th, 2012)

  9. #7
    Junior Member Hazmat210's Avatar
    Join Date
    Feb 2012
    Location
    San Antonio,Tx
    Posts
    23
    My Mood
    Nerdy
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Please help with my while loop that turned into infinite loop!

    How do I do that? I thought that's what the break was for in the switch.

  10. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,110
    Thanks
    65
    Thanked 2,717 Times in 2,667 Posts

    Default Re: Please help with my while loop that turned into infinite loop!

    The break in the switch is for the switch to prevent falling through to the next case. It causes execution to exit the switch statement.
    begin loop
    menu
    do stuff
    end loop

  11. The Following User Says Thank You to Norm For This Useful Post:

    Hazmat210 (March 10th, 2012)

  12. #9
    Junior Member Hazmat210's Avatar
    Join Date
    Feb 2012
    Location
    San Antonio,Tx
    Posts
    23
    My Mood
    Nerdy
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Please help with my while loop that turned into infinite loop!

    Ok, so i need to include the menu inside the while statement I see I will post new code here as soon as I get a chance I really do appreciate the help.

  13. #10
    Junior Member Hazmat210's Avatar
    Join Date
    Feb 2012
    Location
    San Antonio,Tx
    Posts
    23
    My Mood
    Nerdy
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Please help with my while loop that turned into infinite loop!

    So thank you to all who replied. Big thank you too Norm I guess I really need to sit down sketch out the logic and then write the code.
    Here is the rewritten working code.


     
    import java.util.Scanner;
     
    public class Calculator
        {
            public static void main(String[] args)
            {
     
     
            int choice, choice2, choice3;
            float result;
     
            Scanner scan = new Scanner (System.in);
     
     
     
     
            do
                {
     
                    System.out.println("What would you like to do? ");
                    System.out.println("1. Add");
                    System.out.println("2. Subtract");
                    System.out.println("3. Multiply");
                    System.out.println("4. Divide");
                    System.out.println("5. Exit");
                    System.out.print("Selection: ");
                    choice = scan.nextInt();
     
     
                    System.out.print("Enter first number: ");
                    choice2 = scan.nextInt();
                    System.out.print("Enter second number: ");
                    choice3 = scan.nextInt();
     
     
     
                switch (choice)
                    {
                    case 1:
                    //Add 2 integers
     
                    System.out.println(choice2 + "+" + choice3 + "=" + (choice2 + choice3));
                    break;
     
                    case 2:
                    //Subtract 2 integers
     
                    System.out.println(choice2 + "-" + choice3 + "=" + (choice2 - choice3));
                    break;
     
                    case 3:
                    //Multiply 2 integers
     
                    System.out.println(choice2 + "*" + choice3 + "=" + (choice2 * choice3));
                    break;
     
                    case 4:
                    //Divide 2 integers
     
                    result = choice2 / choice3;
                    System.out.println(choice2 + "/" + choice3 + "=" + result);
                    break;
     
                    case 5:
                    //Exit
     
                    default:
     
                    //
                    break;
                    }
                }while (choice != 5);
     
            }
        }

Similar Threads

  1. help me terminate the infinite loop.
    By ab7 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 10th, 2012, 03:40 AM
  2. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 08:57 PM
  3. Confused about infinite loop
    By Lokesh in forum Java Theory & Questions
    Replies: 3
    Last Post: March 9th, 2011, 07:45 AM
  4. [SOLVED] My while loop has run into an infinite loop...?
    By kari4848 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 1st, 2011, 12:05 PM
  5. Doubling hashing, infinite loop?
    By vluong in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 8th, 2009, 01:26 AM