0

I'm working on my midterm project and I've more or less finished. The only problem that I am having is that when I run the program and it asks me to enter the full employee's name, the compiler throws an exception at me about the scanner. I went from Scanner input to "scanner user_input" but it still won't compile properly. Any hint of what the problem is would be appreciated.

package midterm;

import java.util.Scanner;

public class Midterm {

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

        System.out.print("If you wish to enter another's employee's inforamtion"
                        + " please press 1, else to exit enter 0.");
        int choice = user_input.nextInt();

        if (choice == 1) {
            System.out.print("What is the employee's full name. ");
            String empName = user_input.next();
            System.out.printf("Please enter the number of hours that the employee has worked. ");
            double hoursWorked = user_input.nextDouble();
            System.out.printf("Please enter the employee's hourly pay rate. ");
            double payRate = user_input.nextDouble();

            displayPay(empName, calculatePay(hoursWorked, payRate));
        }
        else if (choice == 0) {
            System.exit(0);
        }
    }

    public static double calculatePay(double hours, double pay) {
        double wages = 0;

        if (hours <= 40) {
            wages = hours * pay;
        }

        if (hours > 40) {
            double regPay = hours * pay;
            double overTime = (hours - 40) * pay * 1.5;
            wages = regPay + overTime;
        }

        return wages;
    }

    public static void displayPay(String name, double empWage) {
        System.out.print("-----------------------------------------");
        System.out.print("Employee Name: " + name);
        System.out.print("Pay Check Amount: $" + empWage);
        System.out.print("-----------------------------------------");
    }
}
1
  • What error is shown? Is it a compiler error or a runtime exception? Commented Oct 12, 2013 at 0:06

4 Answers 4

1

The error is here:

System.out.print ("What is the employee's full name. ");
String empName = user_input.next();

next() reads in everything until the next delimiter, which is by default whitespace. So, if someone enters in a first name and a last name (separated by a space), this will only read the first name. Hence, when you call user_input.nextDouble() later, there's still part of the name to be read, and the program croaks because the next token (in this case, the last name), can't be parsed as a double.

Since this sounds like a school project, I won't say exactly how to fix it.

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

Comments

0

Pretty straight forward ! This is how you can use scanner class to accept inputs from the keyboard:

    String str;
    Scanner in = new Scanner(System.in);

    System.out.println("Enter any string :-");
    str = in.nextLine();
    System.out.println(str); // will print the string that you entered.

Comments

0

Try to use:

System.out.print ("What is the employee's full name. ");
String empName = user_input.nextLine();

Comments

0

Don't use user_input.next();

instead of that, try this: user_input.nextLine();

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.