0

I tried something like this but

public static void main(String[] args){
    Scanner Inp = new Scanner(System.in);
    System.out.println("****Bread Store Inventory****");
    System.out.print("Enter the bread amount as the owner of the shop: ");
    int a = Inp.nextInt(); //The bread amount
        if (a < 0) {
        System.out.println("ERROR: Value cannot be negative.");  return;
    }
    System.out.print("Enter the cost of a single bread: ");
    double b = Inp.nextDouble(); //The cost of a single bread
        if (b < 0) {
        System.out.println("ERROR: Value cannot be negative.");  return;
    }
    System.out.println("****Customer User Interface****");
    System.out.print("Welcome to our shop. We have "+a+" breads available. "+"How many would you like? ");
    int c = Inp.nextInt(); //How many breads will the customer buy
        if (c <= 0) {
        System.out.println("ERROR: Value has to be a positive number.");  return;
    }
        else if (a < c) System.out.print("We don't have that many breads at our store, sorry.");
    double d = c*b; //This is cost of the breads that the customer buys.

                if (a >= c) {
                    a -= c;
                    System.out.println("The cost is " +d+".");
                    System.out.println("Thanks for shopping with us today.");
                    System.out.println("We have " +a+ " breads left.");
                }
            }
        }

i cant seem to put a double value for b and i don't know why it says its an inputmismatch but i still couldnt figure it out. Im so new to this so please help me.

  • 1
    Please show an example input. – Federico klez Culloca Oct 19 '22 at 14:01
  • 4
    have you tried to use a comma `,` instead of a decimal point `.` (or vice-versa) ? (it uses the default locale to parse numbers - can be changed using the `useLocale()` method) – user16320675 Oct 19 '22 at 14:04
  • If you get an InputMismatchException is because you're inputting something that is not a Double. For example you're inputting `2b`, or `2,4` (instead of `2.4`) etc. – Matteo NNZ Oct 19 '22 at 14:05
  • 1
    @user16320675 i am embarrassed about this but i've never tried to put comma instead of a decimal so i tried it and it works! Sorry and thank you – Thaelith Oct 19 '22 at 14:07
  • You may want to use more meaningful variable names (e.g. more meaningful than `a` `b` and `c`). They not only help with the documentation but make it easier for others to understand what you are doing. – WJS Oct 19 '22 at 14:12

1 Answers1

0

The problam was the decimal point so i tried using comma instead and it works!

  • 1
    This would best be entered as comment. And you can change the locale if you want to use periods instead of commas. And Welcome to StackOverflow. I recommend you take the [tour] to see how things work here. – WJS Oct 19 '22 at 14:15
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 22 '22 at 00:29