0

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.

5
  • forgot to say, when I run it, it just show the Price, and after I entered the price and tap enter. It comes Exception in thread "main" java.util.InputMismatchException Commented Nov 17, 2019 at 18:50
  • 1
    You should use the full stacktrace to find out what code line caused the problem. (it is likely to be sc.readDouble(), but we should confirm this by looking at the stacktrace) Commented Nov 17, 2019 at 18:56
  • I am did copy and execute you code and no have any exceptions. In which string you get the error? Commented Nov 17, 2019 at 19:00
  • when i ran it it shows this, but the tricky thing is if i enter 372, an integer, it will work really well, but when I enter with decimals, it becomes like this below Price: 372.38 Exception in thread "main" java.util.InputMismatchException at java.base/java.util.Scanner.throwFor(Scanner.java:939) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextDouble(Scanner.java:2564) at xz222bd_assign1.Change.main(Change.java:20) Process finished with exit code 1 Commented Nov 17, 2019 at 19:26
  • SO when I entered comma instead of a point in the price, it worked!!!! Commented Nov 17, 2019 at 19:30

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

YES!!! When I entered comma but not point it WORKED!!!! OMG thank you!!!
@xixn525 I am glad that I helped. Take a look at what should I do when someone answers my question.

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.