user3360708
user3360708

Reputation: 45

Java Try-Catch Exception Calculator

So I'm trying to make a user input calculator using try-catch and exception. The program keeps on repeating and couldn't get the actual input for the numbers and it also includes the statement Wrong input. Please try again.

Any idea on how to fix this?

import java.util.*;
public class Calculator
{
    public static void main(String[] args) {
        int x=1;
        do {
            try {
                System.out.println("Menu");
                System.out.println("1-Addition");
                System.out.println("2-Subtraction");
                System.out.println("3-Multiplication");
                System.out.println("4-Divison");
                System.out.println("5-Modulos");
                System.out.println("6-Exit");
                System.out.println("Choose option: ");
                Scanner scan = new Scanner(System.in);
                int choice = scan.nextInt();  


                switch (choice) {
                    case 1: 
                        System.out.print("Input two numbers:");
                        String dimension = scan.nextLine();
                                String[] parts = dimension.split(" ");
                                int a = Integer.parseInt(parts[0]);
                                int b = Integer.parseInt(parts[1]);
                                int c=a+b;
                                System.out.println("Sum = " +c);
                            break;  
                    case 2: 
                        System.out.print("Input two numbers:");
                        String dif = scan.nextLine();
                                String[] difference = dif.split(" ");
                                int num1 = Integer.parseInt(difference[0]);
                                int num2 = Integer.parseInt(difference[1]);
                                    int d=num1-num2;
                                    System.out.println("Difference = " +d);
                        break;
                    case 3: 
                        System.out.print("Input two numbers:");
                        String multi = scan.nextLine();
                                String[] product = multi.split(" ");
                                int num3 = Integer.parseInt(product[0]);
                                int num4 = Integer.parseInt(product[1]);
                                int p=num3*num4;
                                System.out.println("Product = " +p);
                        break;  
                    case 4: 
                        System.out.print("Input two numbers:");
                        String div = scan.nextLine();
                                String[] quotient = div.split(" ");
                                int num5 = Integer.parseInt(quotient[0]);
                                int num6 = Integer.parseInt(quotient[1]);
                                int q=num5/num6;
                                System.out.println("Quotient = " +q);
                         break;
                    case 5: 
                        System.out.print("Input two numbers:");
                        String mod = scan.nextLine();
                                String[] modulo = mod.split(" ");
                                int num7 = Integer.parseInt(modulo[0]);
                                int num8 = Integer.parseInt(modulo[1]);
                                int m=num7%num8;
                                System.out.println("Modulos = " +m);
                        break;
                    case 6: 
                        System.out.println("Now exiting program...");
                        break;  
                }
            } catch (Exception e) {
               System.out.println("Wrong input. Try again.");
            }
        } while (x==1);
    } 
}

Upvotes: 2

Views: 19539

Answers (3)

Marcin Skiba
Marcin Skiba

Reputation: 103

You should add:

scan.skip("\n");

right after

int choice = scan.nextInt();

Why? Because nextInt() reads next integer from input. And it leaves new line character at the end on the input. It's like:

1<enter>

After that you invoke scan.nextLine() which reads everything up to next new line character. In fact there is 1 new line character in the buffer. And here your nextLine() reads an empty string.

To solve that issue you have to skip buffered new line characters with scan.skip("\n").

EDIT: in addition, your code doesn't allow you to exit, because you're not changing your x variable. Try to change it after System.out.println("Now exiting program...");. It will work ;)

Upvotes: 2

AJ.
AJ.

Reputation: 4534

You need to do this

int choice = Integer.parseInt(scan.nextLine());

because you are reading next input with readline()

String dimension = scan.nextLine();

and \n is already present it the stream when you enter your option with nextInt() because nextint() never reads the \n which you leave behind by pressing Enter button.

Upvotes: 3

Leos Literak
Leos Literak

Reputation: 9474

This will help you to identify root cause:

} catch (Exception e) {
    System.out.println("Wrong input. Try again.");
    e.printStackTrace();
}

Upvotes: 1

Related Questions