Java scanner exception issue

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("-----------------------------------------");
    }
}

Upvotes: 0

Views: 109

Answers (4)

Manish Tuteja
Manish Tuteja

Reputation: 205

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.

Upvotes: 0

t1w
t1w

Reputation: 1428

Don't use user_input.next();

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

Upvotes: 0

Sk1X1
Sk1X1

Reputation: 1373

Try to use:

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

Upvotes: 0

Dennis Meng
Dennis Meng

Reputation: 5187

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.

Upvotes: 1

Related Questions