简体   繁体   中英

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

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.

Try to use:

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

Don't use user_input.next();

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM