0

I'm stuck on an error for hours while practising Java. When I enter the price with decimal points I get this Error

Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:864)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextDouble(Scanner.java:2413)
    at grocerystore.GroceryStore.main(GroceryStore.java:19)
C:\Users\aslan\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: 

My code:

package grocerystore;
import java.util.Scanner;
/**
 *
 * @author aslan
 */
public class GroceryStore {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        double [] prices = new double [5];
        Scanner in = new Scanner(System.in);
        System.out.println("Enter 5 prices: ");
        prices[0] = in.nextDouble();
        prices[1] = in.nextDouble();
        prices[2] = in.nextDouble();
        prices[3] = in.nextDouble();
        prices[4] = in.nextDouble();
        double total = prices[0] + prices[1] + prices[2] + prices[4];
        System.out.printf("the tot1al of all 5 items is:$%5.2f" +total);
    }
}

Can any one help ???

Robert Jack Will
  • 10,333
  • 1
  • 21
  • 29
Aslan Ali
  • 3
  • 1
  • 1
    Give an example of what you are inputting; any number formatted as x.y should work. – Sean May 24 '18 at 18:04
  • System.out.printf("the tot1al of all 5 items is:$%5.2f" +total); Changed it to System.out.println("the total of all 5 items is: +total"); but the same problem occurs . IM entering x.y or x.yy but it doesn't work – Aslan Ali May 24 '18 at 18:06
  • 1
    @AslanAli I did not mean x.y literally-put in one number followed by a decimal and then another number. examples include 2.2, 31.0, and 11.2 – Sean May 24 '18 at 18:10
  • Try to use comma instead of dot in input (like 12,38) and see what happens; BTW, is there any possible reason why total doesn't add `prices[3]`? – zlakad May 24 '18 at 18:11
  • the comma works !!! thanks :) – Aslan Ali May 24 '18 at 18:12

1 Answers1

1

Anything other than a number or a full stop for in.nextDouble() will throw an InputMismatchException. So try to ensure that you only put numbers and full stops in.

Also, printf()'s arguments are a format string and then the arguments. Since you have a format specifier in the String you have supplied, it must have an argument to match. So the correct syntax is .out.printf("the tot1al of all 5 items is:$%5.2f", total);

Also, I notice that you missed prices[3]. A working solution:

        double[] prices = new double[5];

        Scanner in = new Scanner(System.in);
        System.out.println("Enter 5 prices: ");

        prices[0] = in.nextDouble();
        prices[1] = in.nextDouble();
        prices[2] = in.nextDouble();
        prices[3] = in.nextDouble();
        prices[4] = in.nextDouble();

        double total = prices[0] + prices[1] + prices[2] + prices[3] + prices[4];

        System.out.printf("the tot1al of all 5 items is:$%5.2f", total);


Output:

    Enter 5 prices: 
    .0
    .1
    .2
    .3
    .4
    the tot1al of all 5 items is:$ 1.00
SirCipher
  • 933
  • 1
  • 7
  • 16