xixn525
xixn525

Reputation: 1

Java Exception in thread "main" java.util.InputMismatchException

This is a change problem, I need to enter the price and payment, then it will compute the change a customer should receive when he has paid a certain sum(in coins and bills).

import java.util.Scanner;

public class Change {
    public static void main(String[] args) {
        int change;
        int a; //a stands for 1000 kr bills.
        int b; //b stands for 500 kr bills.
        int c; //c stands for 200 kr bills.
        int d; //c stands for 100 kr bills.
        int e; //e stands for 50 kr bills.
        int f; //f stands for 20 kr bills.
        int g; //g stands for 10 kr coins.
        int h; //h stands for 5 kr coins.
        int i; //i stands for 2 kr coins.
        int j; //j stands for 1 kr coins.
        Scanner sc = new Scanner(System.in);
        System.out.println("Price:");
        double p = sc.nextDouble(); // p stands for the price.
        System.out.println("Payment:");
        int k = sc.nextInt(); //k stands for the payment.
        int q = (int)Math.round(p);
        change = k-q;
        a = (k-q)/1000;
        b = ((k-q)-(a*1000))/500;
        c = ((k-q)-(a*1000+b*500))/200;
        d = ((k-q)-(a*1000+b*500+c*200))/100;
        e = ((k-q)-(a*1000+b*500+c*200+d*100))/50;
        f = ((k-q)-(a*1000+b*500+c*200+d*100+e*50))/20;
        g = ((k-q)-(a*1000+b*500+c*200+d*100+e*50+f*20))/10;
        h = ((k-q)-(a*1000+b*500+c*200+d*100+e*50+f*20+g*10))/5;
        i = ((k-q)-(a*1000+b*500+c*200+d*100+e*50+f*20+g*10+h*5))/2;
        j = ((k-q)-(a*1000+b*500+c*200+d*100+e*50+f*20+g*10+h*5+i*2))/1;
        System.out.println("Change:"+change);
        System.out.println("1000 kr bills:"+a);
        System.out.println("500 kr bills:"+b);
        System.out.println("200 kr bills:"+c);
        System.out.println("100 kr bills:"+d);
        System.out.println("50 kr bills:"+e);
        System.out.println("20 kr bills:"+f);
        System.out.println("10 kr coins:"+g);
        System.out.println("5 kr coins:"+h);
        System.out.println("2 kr coins:"+i);
        System.out.println("1 kr coins:"+j);

    }
}

When I run it, it just show the Price, and after I entered the price and tap enter it shows Exception in thread "main" java.util.InputMismatchException.

Upvotes: 0

Views: 740

Answers (1)

George Z.
George Z.

Reputation: 6808

When you double p = sc.nextDouble();, but you give as input something that is not a double, you are getting the InputMismatchException. For example, if you give as input "5g", it will throw the exception.

(This thing applies to all scanner.next<something>. If you int number = sc.nextInt() and you give a non Integer value you will get the exception as well.)

If you are sure that you are giving a double variable input (e.g 12.58) and you are still getting the exception, try to give it with comma, like 12,58. And of course, the opposite of that.

Scanner reads the separator based on default Locale.

If you want to make sure that the dot (.) will be used to separate non integer numbers you can change the default locale:

Locale.setDefault(Locale.US);

Upvotes: 2

Related Questions