Legendary Ksb2806
Legendary Ksb2806

Reputation: 3

Exception in Thread "main" java.util.Input MismatchException

I'm working on my homework assignment for a class. You have to calculate the salary of the month. Everytime I try to run it, it keeps saying this: How can I fix this? 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.nextInt(Scanner.java:2117)

at java.util.Scanner.nextInt(Scanner.java: 2076)

at D2.main(D2.java:23)

import java.util.Scanner;

public class D2
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);

        int L1Cs = 80;
        int L2Cs = 120;
        int L3Cs = 160;
        int L1Ct = 100;
        int L2Ct = 140;
        int L3Ct = 180;
        int L1Csv = 100;
        int L2Csv = 150;
        int L3Csv = 200;
        int BSalary = 3000;
        int Stotal, Ttotal, SVtotal;

        System.out.println("Please enter your name");
        int name = sc.nextInt();

        System.out.println("How many sedans have you sold this month");
        int Sedans = sc.nextInt();

        System.out.println("How many trucks have you sold this month");
        int Trucks = sc.nextInt();

        System.out.println("How many SUVs have you sold this month");
        int SUVs = sc.nextInt();


        if(Sedans <= 10)
        {
            Stotal = (L1Cs * Sedans);
        }
        else if (Sedans >= 11 && Sedans <=20)
        {
            Stotal = (L1Cs*10 +(Sedans - 10) * L2Cs);
        }

        else
        {
            Stotal = (L1Cs*10 +L1Cs *10 + (Sedans - 10) * L3Cs);
        }

        System.out.println("Your comission for the sedans is:" + Stotal);


        if(Trucks <= 10)
        {
            Ttotal = (L1Cs * Trucks);
        }
        else if (Trucks >= 11 && Trucks <=20)
        {
            Ttotal = (L1Ct*10 + (Trucks - 10) * L2Ct);
        }
        else
        {
            Ttotal = (L1Ct*10 + L2Ct *10 + (Trucks - 10) * L3Ct);
        }
        System.out.println("Your comission for the trucks is:" + Ttotal);


        if(SUVs <= 10)
        {
            SVtotal = (L1Cs * SUVs);
        }
            else if (SUVs >= 11 && SUVs <=20)
        {
            SVtotal = (L1Csv*10 + (SUVs  - 10) * L2Csv);
        }
        else
        {
            SVtotal = (L1Csv*10 + L2Csv *10 + (SUVs - 10) * L3Csv);
        }
        System.out.println("Your comission for the SUVs is:" + SVtotal);

    double comission = (Stotal + Ttotal + SVtotal);
    double MSalary = (comission + 3000);
    }
}

Upvotes: 0

Views: 123

Answers (1)

Daniel Nguyen
Daniel Nguyen

Reputation: 429

The prompt it seems to be failing on is:

"Please enter your name"

If you are entering in your name, instead of an integer, that's why it is throwing an exception. The scanner would be looking for an integer, which your name is not. You might want to replace:

int name = sc.nextInt();

with:

String name = sc.next();

or something similar to get name as a String.

Upvotes: 1

Related Questions