0

I am a novice java student I need to caculate employee bonus's based on their units produced. Whenever I run the program it is giving a scanner error due to the input. "java.util.InputMismatchException". Any help is greatly appreciated!

 /**
          * NAME: Mitchell Noble
          * DATE: October 27, 2015
          * FILE: lab9
          * COMMENTS: This java program is designed to caculate employee bonus's based on their units produced
          */
           import java.util.Scanner;

        public class lab9
        {
           public static void main(String[]args)
           {
             // declare variables
             double Lastname;
             double Currentunits;
             double Lastunits;
             double Firstname;

             //Create a Scanner Object
              Scanner keyboard= new Scanner(System.in);

             //Get names and units
             System.out.print("Hello we are going to learn about your production this year and award bonus's but first we need some information.");
             System.out.print("What is your last name?");
             Lastname = keyboard.nextDouble();
             System.out.print("What is your first name?");
             Firstname = keyboard.nextDouble();
             System.out.print("How many units did you produce this year?");
             Currentunits = keyboard.nextDouble();
             System.out.print("How many units did you produce last year?");
             Lastunits = keyboard.nextDouble();
             //Sort into proper bonus category
            if (Currentunits > Lastunits)
              {
                 if (Currentunits >= 1000)
                 {
                    if (Currentunits >= 3001)
                    {
                       if (Currentunits >= 6001)
                       {
                         System.out.print(Firstname + Lastname + "Based on your units prodcued this year your bonus will be $200.  Good work!");
                       }
                       else
                       {
                         System.out.print(Firstname + Lastname + "Based on your units prodcued this year your bonus will be $100.  Good work!");
                       }
                    }
                    else
                    {
                       System.out.print(Firstname + Lastname + "Based on your units prodcued this year your bonus will be $50.  Good work!");
                    }
                 }
                 else
                 {
                    System.out.print(Firstname + Lastname + "Based on your units produced this year your bonus will be $25. Good work!");
                 }
              }
              else
              {
                System.out.print(Firstname + Lastname + "Based on the units produced this year compared to last year you do not qualify for any bonu's.");
              }
           } // close main
        } // close lab7
4
  • 1
    You tried entering something into the console which can't be parsed as a double. Can you show us your input? Commented Oct 28, 2015 at 6:20
  • 1
    You are expecting values like lastName , firstName to be double. Check your code. I think you have to use nextLine() and declare those fields as Strings Commented Oct 28, 2015 at 6:21
  • why did you declare firstName and LastName as double instead of String? Commented Oct 28, 2015 at 6:21
  • As a side note your logic is not optimal. Instead of nesting your ifs like that, you should try if(currentUnits >= 6001) { // $200 bonus } else if(currentUnits >= 3001) { // $100 bonus } etc. Makes the code a lot clearer. Commented Oct 28, 2015 at 7:35

3 Answers 3

2

Why are you reading a double for last name?

         double Lastname;

Define it as string like this:

String Lastname;

and change your code to read the Lastname from

         Lastname = keyboard.nextDouble();

to read string using nextLine, like this:

         Lastname = keyboard.nextLine();

Same applies to firstName

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks so much for the help guys greatly appreciated!
OP will run into the next issue with this, namely mixing nextXXX with nextLine. He should always only use nextLine since the user is actually entering lines always, not only numbers (he is pressing enter).
0

Try with below code

 String Lastname;
     double Currentunits;
     double Lastunits;
     String Firstname;

     //Create a Scanner Object
      Scanner keyboard= new Scanner(System.in);

     //Get names and units
     System.out.print("Hello we are going to learn about your production this year and award bonus's but first we need some information.");
     System.out.print("What is your last name?");
     Lastname = keyboard.nextLine();
     System.out.print("What is your first name?");
     Firstname = keyboard.nextLine();
     System.out.print("How many units did you produce this year?");
     Currentunits = keyboard.nextDouble();
     System.out.print("How many units did you produce last year?");
     Lastunits = keyboard.nextDouble();
     //Sort into proper bonus category
    if (Currentunits > Lastunits)
      {
         if (Currentunits >= 1000)
         {
            if (Currentunits >= 3001)
            {
               if (Currentunits >= 6001)
               {
                 System.out.print(Firstname + Lastname + "Based on your units prodcued this year your bonus will be $200.  Good work!");
               }
               else
               {
                 System.out.print(Firstname + Lastname + "Based on your units prodcued this year your bonus will be $100.  Good work!");
               }
            }
            else
            {
               System.out.print(Firstname + Lastname + "Based on your units prodcued this year your bonus will be $50.  Good work!");
            }
         }
         else
         {
            System.out.print(Firstname + Lastname + "Based on your units produced this year your bonus will be $25. Good work!");
         }
      }
      else
      {
        System.out.print(Firstname + Lastname + "Based on the units produced this year compared to last year you do not qualify for any bonu's.");
      }

1 Comment

You have to declare lastName & firstName as String and have to use nextLine() to get input from user.
0

By definition, the double data type is a double-precision 64-bit IEEE 754 floating point. Its range of values is 4.94065645841246544e-324d to 1.79769313486231570e+308d. It does not stand for two names! So you really want to set firstName and lastName as String.

Comments

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.