0

I've made a basic calculator program and I'm getting this exception:

java.util.InputMismatchException java.util.Scanner.next(Unknown Source)

The code runs just fine but when exception occurs it doesn't allows the user to input using Scanner. What am I doing wrong and how can I fix it?

package string;

import java.util.Scanner;
import java.lang.Exception;

public class Calculator {

double sum(double a,double b)
{
    double c =a+b;
    return c;
}

double subtract(double a,double b)
{
    double c= a-b;
    return c;
}

double multiply(double a,double b)
{
    double c=a*b;
    return c;
}

double divide(double a,double b)
{
    double c=a/b;
    return  c;
}


public static void main(String[] args) {
Calculator f= new Calculator();
int choice;
int z;

Scanner s1 =new Scanner(System.in);


do{
    try{

System.out.println("Welcome To Mini Calculator:  Which Function Do You Want To Use");
System.out.println("1.Addition");
System.out.println("2.Subtraction");
System.out.println("3.Multiplication");
System.out.println("4.Division");
System.out.println();
System.out.print("Please Enter Your Choice Number: ");
choice = s1.nextInt();

System.out.println();

switch(choice){
case 1: 
    System.out.print("Please Enter The First Number: ");
    double x= s1.nextDouble();
    System.out.println();
    System.out.print("Please Enter The Second Number: ");
    double y= s1.nextDouble();
    double u = f.sum(x,y);
    System.out.println();
    System.out.println("The Sum Of Two Numbers is: " + u);
    break;
case 2:
    System.out.print("Please Enter The First Number: ");
    double q= s1.nextDouble();
    System.out.println();
    System.out.print("Please Enter The Second Number: ");
    double w= s1.nextDouble();
    double i= f.subtract(q,w);
    System.out.println();
    System.out.println("The Substraction Of Two Numbers is: "+i );
    break;
case 3:
    System.out.print("Please Enter The First Number: ");
    double e= s1.nextDouble();
    System.out.println();
    System.out.print("Please Enter The Second Number: ");
    double r= s1.nextDouble();
    double o= f.multiply(e, r);
    System.out.println();
    System.out.println("The Multiplication Of Two Numbers " + o);
    break;
case 4:
    System.out.print("Please Enter The First Number: ");
    double t= s1.nextDouble();
    System.out.println();
    System.out.print("Please Enter The Second Number: ");
    double k= s1.nextDouble(); 
    double p= f.divide(t,k);
    System.out.println();
    System.out.println("The Divison of Two Numbers is: "+ p);
    break;
default:System.out.println(); 
    System.out.println("Please Enter a Valid Choice from 1 to 4");
}
}
catch(Exception e) {
    System.out.println("Input error: You have entered wrong input");
    System.out.println("Please restart the program");

    }
    System.out.println();
    System.out.println("Do You Want To perform Another Functionality?");
    System.out.println("Press 1 to Continue and Press 2 to Terminate The Program");
    z= s1.nextInt();  // Issue comes here. It runs fine without exception. When exception occurs in above code ,it doesn't take input and shows another exception
}
while(z==1);

System.out.println();
System.out.println("Thank You For Using Calculator");
s1.close();

}
}
  • What did you try as your input? – konsolas Aug 31 '17 at 16:58
  • I tried, it worked for me – azro Aug 31 '17 at 16:59
  • That's because the `nextFoo()` method that is throwing the exception doesn't consume the input, so the `nextInt()` call in your catch block is choking on the same bad input. See https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo for more details. – azurefrog Aug 31 '17 at 17:01
  • 1
    Also, don't use exceptions for flow control in general, instead use `hasNextFoo()` to check for valid input before trying to read it. – azurefrog Aug 31 '17 at 17:01

1 Answers1

1

When you enter a wrong input, it goes in the catch but the input is still here, so z= s1.nextInt(); throws another exception which is not catched and it crashes

So you need to read the input in the catch, to clear the scanner :

} catch (Exception e) {
    System.out.println("Input error: You have entered wrong input");
    System.out.println("Please restart the program");
    s1.nextLine();
}

Also, you have a lot of code duplicate, and variable names which means nothing, this is not very good compare to standards, I would suggestsomething like this to replace your whole switch{ ... }

System.out.println();
System.out.print("Please Enter The First Number: ");
double numb1 = s1.nextDouble();
System.out.println();
System.out.print("Please Enter The Second Number: ");
double numb2 = s1.nextDouble();
double res;
String operation = "";

switch (choice) {
    case 1:
        res = f.sum(numb1, numb2);
        operation = "Sum";
        break;
    case 2:
        res = f.subtract(numb1, numb2);
        operation = "Substraction";
        break;
    case 3:
        res = f.multiply(numb1, numb2);
        operation = "Multiplication";
        break;
    case 4:
        res = f.divide(numb1, numb2);
        operation = "Divison";
        break;
    default:
        res = 0;
        System.out.println();
        System.out.println("Please Enter a Valid Choice from 1 to 4");
}

System.out.println();
System.out.println("The " + operation + " Of Two Numbers is: " + res);
azro
  • 35,213
  • 7
  • 25
  • 55
  • azro - Thank you so much for the clarification. It makes sense now. I have read like 15 posts for this kind of issue on stackoverflow yet didn't find any explanation, only solutions. Thanks for clarifying the reason behind the issue and its solution :) Much appreciated. – Tarunjeet Singh Salh Aug 31 '17 at 17:12
  • @TarunjeetSinghSalh no prob ;) and remember to avoid code duplicate, when you see 2-3 lines identical, look for get only once these ;) also think about vote-up/accept this answer ;) – azro Aug 31 '17 at 17:15
  • Thanks for the tip about code format and maintaining a good standard. I have up voted it. My reputation is below 15 as i have joined just now. My vote is recorded but not visible . It will be so i will be on stackflow from now on :) Thanks once again good sir, – Tarunjeet Singh Salh Aug 31 '17 at 17:40
  • @TarunjeetSinghSalh you can accept the answer, it's better than vote up ;) – azro Aug 31 '17 at 18:14