Gerald Loo Gerald Loo - 4 months ago 39
Java Question

Get the weekly pay

Exception in thread "main" java.util.InputMismatchException

at java.util.Scanner.throwFor(


at java.util.Scanner.nextInt(

at java.util.Scanner.nextInt(

at Commission2.main(

It was working well at the beginning, until everything came crashing down. Now I need help. Anyone?

import java.util.Scanner;//program needs scaanner
import java.text.DecimalFormat;

public class Commission2

public static void main(String args[])


//create Scanner
Scanner input = new Scanner(;

//format decimal with two digits
DecimalFormat twoDigits = new DecimalFormat("0.00");
//format decimal with three digits
DecimalFormat threeDigits = new DecimalFormat("0.000");

//declare all variables
int size,count = 0;
int pay = 200;
double commission = 9/100;
double result = 0;
double item, itemtotal = 0;

//get the limit of the data entry (data validation technique)
System.out.printf("Enter the number of items :");
size = input.nextInt();

}while(size < 0);

//data entry
while (count < size) {

System.out.print("Enter price of item #" +(count + 1) +": ");
item = input.nextInt();

itemtotal += item;

}//end while
result = (itemtotal * commission) + pay;

System.out.printf("%s%d\n","The total earnings for this week is $",result);



On line 25 you are declaring double item, yet on line 38 you are trying to assign input.nextInt() to the variable item.

If you were to provide an Integer value (5) here, the code should work as intended. However when a Double is provided (5,12) an InputMismatchException exception will be thrown.

To fix this simply change this:

item = input.nextInt();

to this:

item = input.nextDouble();