#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0

    Loop keep goin infinite


    hi all i just need a little help in this code im working on the.
    i enter 2 numbers for example 1 and 2 and then a to add but then the program keeps displaying 3 until i stop it.. i need to make the program repeat and display the menu again until i type e to exit so please can anyone help i need to hand this tomorrow :/..oh and i am only allowed to use while loop not do while.

    1. Write a program that displays the following menu to the user:
    Enter: a to add two numbers, s to subtract two numbers, m to multiply two numbers, d to divide two
    numbers, and e to exit.
    Then the program should read two numbers from the user, and display:
    Their sum if the user typed a or A as a choice
    Their difference if the user typed s or S as a choice
    Their product if the user typed m or M as a choice
    Their quotient if the user typed d or D as a choice
    Your program should do appropriate checking in case of division (no division by zero).
    Your program should continuously ask the user for a choice until the user enters E or e.

    so far ive reached this:

    public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter the first and second number");
    int num1 = scan.nextInt();
    int num2 = scan.nextInt();
    System.out.println("============================");
    System.out.println("1.enter a or A to add");
    System.out.println("2.enter s or S to subtract");
    System.out.println("3.enter m or M to multiply");
    System.out.println("4.enter d or D to divide");
    System.out.println("4.enter e or E to exit");
    System.out.println("============================");
    int option = scan.next().charAt(0);


    while(option!='e'){
    switch (option) {
    case 'a':
    case 'A':
    int add = num1 + num2;
    System.out.println(+add);
    break;

    case 's':
    case 'S':
    int sub = num1 - num2;
    System.out.println(+sub);
    break;

    case 'm':
    case 'M':
    int mult = num1 * num2;
    System.out.println(+mult);
    break;

    case 'd':
    case 'D':
    double div = num1 / num2;
    System.out.println(+div);
    if(num2==0){
    System.out.println("impossible");
    break;
    }
    break;
    case 'e':
    case'E':
    System.out.println("exit");
    break;
    }


    }

    }
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    62
    Rep Power
    4
    Java Code:
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);  
            System.out.println("Enter the first and second number");
            int num1 = scan.nextInt();
            int num2 = scan.nextInt();
            System.out.println("============================");
            System.out.println("1.enter a or A to add");
            System.out.println("2.enter s or S to subtract");
            System.out.println("3.enter m or M to multiply");
            System.out.println("4.enter d or D to divide");
            System.out.println("5.enter e or E to exit");
            System.out.println("============================");
            int option = scan.next().charAt(0);
     
     
       while(option!='e'){
            switch (option) {
                case 'a':
                case 'A':
                    int add = num1 + num2;
                    System.out.println(+add);
                    break;
     
                case 's':
                case 'S':
                    int sub = num1 - num2;
                    System.out.println(+sub);
                    break;
     
                case 'm':
                case 'M':
                    int mult = num1 * num2;
                    System.out.println(+mult);
                    break;
     
                case 'd':
                case 'D':
                    double div = num1 / num2;
                    System.out.println(+div);
                    if(num2==0){
                        System.out.println("impossible");
                                break;
                    }
                    break;
                case 'e':
                    case'E':
                        System.out.println("exit");
                        break;
            }
     
     
        }
     
        }
    }

    Hi,

    Firstly, please use the highlight tag, and secondly, it looks to me as though your opening and closing braces are not lined up correctly (see your example above). This is bad practice and can lead to problems later on.

    I don't know why people use switch/case so much? What am I missing here?

    Anyway, here is your code properly lined up and also with some comments as to what you should be doing:
    Java Code:
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            System.out.println("Enter the first and second number");
            // When you are scanning in numbers, you need to try and catch the exceptions
            // incase of any typos - if you enter in an illegal character, it'll cause an
            // error
            int num1 = scan.nextInt();
            int num2 = scan.nextInt();
            // This might cause some confusion - you're asking for character inputs but
            // have listed them as numbers - some users will enter 1 to add, for instance.
            System.out.println("============================");
            System.out.println("1.enter a or A to add");
            System.out.println("2.enter s or S to subtract");
            System.out.println("3.enter m or M to multiply");
            System.out.println("4.enter d or D to divide");
            System.out.println("5.enter e or E to exit");
            System.out.println("============================");
            // This was int, and should be char - it needs to be *inside* the while loop:
            char option = scan.next().charAt(0);
            while(option!='e' || option!='E'){
                switch (option) {
                    case 'a':
                    case 'A':
                        int add = num1 + num2;
                        System.out.println(add);
                        break;
     
                    case 's':
                    case 'S':
                        int sub = num1 - num2;
                        System.out.println(sub);
                        break;
     
                    case 'm':
                    case 'M':
                        int mult = num1 * num2;
                        System.out.println(mult);
                        break;
     
                    case 'd':
                    case 'D':
                        double div = num1 / num2;
                        System.out.println(+div);
                    if(num2==0) {
                        // You are checking for division by zero *after* the calculation
                        // Also, you should use System.err.println
                        System.out.println("impossible");
                        // Your break is inside the if statement, it shouldn't be!
                        break;
                    }
                        // The above break is not needed
                        break;
     
                    case 'e':
                    case'E':
                        System.out.println("exit");
                        break;
                }
            }
        }

    The infinite loop is happening because the variable that reads in the character entered is outside of the while loop, therefore once the while loop starts, the condition is always true.

    Regards,

    Shaun.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0
    thanks alot for replying quickly but the program is displaying continuous output.for example when i enter 1 and 2 and a the program outputs:
    3
    3
    3
    3
    3
    3
    3
    3
    all the way till i stop running it

    any help?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    62
    Rep Power
    4
    Originally Posted by psycho21232
    thanks alot for replying quickly but the program is displaying continuous output.for example when i enter 1 and 2 and a the program outputs:
    3
    3
    3
    3
    3
    3
    3
    3
    all the way till i stop running it

    any help?
    You need to think about the logic of using a while loop - why are you using a while loop? Is there a better way of doing so...?

    Anyway, I wouldn't ever approach this problem in the way that you have, but if I had to because of some assignment criteria, here's how I would have done it:
    Java Code:
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            System.out.println("Enter the first and second number");
            // When you are scanning in numbers, you need to try and catch the exceptions
            // in case of any typos - if you enter in an illegal character, it'll cause an
            // error
            int num1 = 0;
            int num2 = 0;
            try {
                num1 = scan.nextInt();
                num2 = scan.nextInt();
            }
            catch(Exception e) {
                System.err.println(e);
                System.exit(0);
            }
            System.out.println("Enter a or A to add the numbers");
            System.out.println("Enter s or S to subtract the second number from the first");
            System.out.println("Enter m or M to multiply the two entered numbers");
            System.out.println("Enter d or D to divide the the first number by the second");
            System.out.println("Enter e or E to to restart the example");
            System.out.println("Enter any other character to exit program");
            // This was int, and should be char - it needs to be *inside* the while loop:
            char option = scan.next().charAt(0);
            do{
                switch (option) {
                    case 'a':
                    case 'A':
                        int add = num1 + num2;
                        System.out.println(add);
                        break;
     
                    case 's':
                    case 'S':
                        int sub = num1 - num2;
                        System.out.println(sub);
                        break;
     
                    case 'm':
                    case 'M':
                        int mult = num1 * num2;
                        System.out.println(mult);
                        break;
     
                    case 'd':
                    case 'D':
                        if(num2==0) {
                        System.err.println("Division by zero not possible");
                        break;
                    }
                        double div = num1 / num2;
                        System.out.println(+div);
                        // The above break is now relevant because you're breaking before trying to divide by zero
                        break;
     
                    case 'e':
                    case 'E':
                        System.out.println("Restart");
                        main(null);
                        break;
                    default:
                        System.out.println("Good bye");
                        System.exit(0);
                }
                option = scan.next().charAt(0);
            }while((byte)option>0);
        }

    I hope that this helps - please read through the code and try to understand each step, or ask some questions.

    Regards,

    Shaun.
    Last edited by Shaun_B; November 5th, 2012 at 08:04 AM.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0
    Thank you
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    62
    Rep Power
    4
    Can you see where you went wrong now? Basically, the while loop was always true because there was no exit condition, nor did you clear the variable within the loop after it had been used. Your exit condition would simply print out exit without exiting. And overall it wasn't very user friendly. Those were the main points anyway.

    But there are better ways of solving this problem that you should consider as well. Try them.

    Regards,

    Shaun.

IMN logo majestic logo threadwatch logo seochat tools logo