2

Today I was trying to do a basic exercise and I faced in this problem :

Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextDouble(Unknown Source)
    at Main.main(Main.java:14)

This is the code :

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double salary = 0;      // Salary excluded tax
        int tax = 0;            // % of tax
        double taxTot = 0;      // amount of tax
        double totSalary = 0;   // Salary with tax

        System.out.println("Salary, please : "); //Input salary
        salary = input.nextDouble();    

        if (salary <= 15000) {                          // <=15000
            tax = 10;
        } else if (salary>= 40000 && salary < 60000){       // >=40000
            tax = 20;
        } else {                                            // over > 60000
            tax = 30;
        }

        taxTot = salary / 100*tax;
        totSalary = salary - taxTot;
        System.out.println("Your tax is : " + taxTot + " Your salary : " + totSalary);
    }
}
4
  • 1
    I compiled and ran your program. I got no error when I entered a number. However when entering a letter I got the same error as you, which is not surprising. What is your input? Commented Apr 19, 2016 at 20:14
  • i cannot seem to replicate your problem. copy and pasted and it works fine for me. Commented Apr 19, 2016 at 20:14
  • What kind of input are you providing when you run this application ? Commented Apr 19, 2016 at 20:15
  • Ahh i got it, yeah now I understand the input problem. Commented Apr 22, 2016 at 15:04

3 Answers 3

2

An java.util.InputMismatchException can get thrown if the next input in the Scanner doesn't match the type you're trying to get. Here's an example:

Scanner input = new Scanner("hello");
double salary = input.nextDouble();

So the problem most probably originates from the salary = input.nextDouble(); line in your code, and the cause is that you didn't enter a valid double.

To test the behavior of your program, you can write the input in the Scanner constructor like I wrote earlier above. For example you can test by writing this:

Scanner input = new Scanner("9000");

So that salary will be 9000, and so since salary <= 15000, tax will be set to 10. Change the value to something else to get a different outcome, for example:

Scanner input = new Scanner("41000");

When you're comfortable with working with a Scanner, you can change the fixed string parameter back to new Scanner(System.in) and run your complete program.

Sign up to request clarification or add additional context in comments.

1 Comment

I tried as you said but i get error when i try to type a decimal number, i mean if i type 13.548 i get error and the program stop to run. However when i type integer number the program works perfectly.
2

Try for example

Scanner scanner = new Scanner(System.in).useLocale(Locale.US);

There is a difference between 5,0 and 5.0

1 Comment

thanks a lot. I understand now where was the problem.
0

If you have a read through this it tells you about the InputMismatchException

Basically, the input in your program doesn't match the Scanner type. So you might get the error because your input is not a double

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.