-1

I'm making a payroll program and one of the options is to check data on current users. I want to allow the user to enter either the employee's ID or name to retrieve this information.

Scanner userChoice = new Scanner(System.in);
System.out.println("Enter employee ID or full name");
String checkHEmp = userChoice.next();
int checkHempID = userChoice.nextInt(); 
    if (checkHempID ==3|| checkHEmp == "Jill Jones") {
        HourlyEmployee jones = new HourlyEmployee();
        jones.jJones();
    }

In this example HourlyEmployee is a class of hourly employees and jJones() is a method in that class that contains the information for one of the employees. When I enter either "3" (the ID) or "Jill Jones" nothing happens afterwards.

Edit: Added method for jJones()

public void jJones() {
    double hourlyWagesJones = 16.45;
    double hoursWorkedJones = 48;

    System.out.println("Employee ID: 3");
    System.out.println("Last name: Jones");
    System.out.println("First name: Jill");
    System.out.println("Date Hired: 1/22/2011");
    System.out.println("Hours Worked: " + hoursWorkedJones);
    System.out.println("Hourly Wages: $" + hourlyWagesJones);
    double totalEarnedJones = new Double(hourlyWagesJones * hoursWorkedJones);
    System.out.println("Total earned: $" + totalEarnedJones);   
}
  • Entering the number 3 should work so if that actually is a problem then you should clarify your question by stating your input, output, the contents of the `jJones` method and giving us a reproduceable example. – Jeroen Vannevel Aug 10 '14 at 22:28
  • in addition, print `checkHEmp` and see what you get – Ben Aug 10 '14 at 22:30
  • If you input 3 it goes into the `checkHEmp` variable which you are checking if it is `"Jill Jones"`. – takendarkk Aug 10 '14 at 22:30
  • I changed the string conditional to .equals instead but I am still getting the same problem. I'm getting [this](http://imgur.com/6KJsDXe) in Debug. – SeverelyConfused Aug 10 '14 at 22:30
  • Let me guess: you're entering "Jill Jones" first and "3" second? – Jeroen Vannevel Aug 10 '14 at 22:32
  • When I print out checkHEmp, only the first name gets printed before the program crashes and enters Debug Scanner is imported. Even when I only enter "3" nothing happens. I have the code written to print both checkHEmp (string) and checkHempID (int) but neither prints – SeverelyConfused Aug 10 '14 at 22:39
  • I would add that `checkHEmp` is a pretty bad variable name - pretty easy to glance at it the wrong way and think it's `checkEmp`. Why not `checkHourlyEmployee`? – James Kingsbery Aug 11 '14 at 16:09

1 Answers1

4

Ok so,

String checkHEmp = userChoice.next();

is a expecting a String and the String you enter is "Jill Jones", but Scanner's default delimiter is " " which means that it will read up to the first " " it finds, so only the Jill will be stored in checkHEmp which of course is not what you check for in your if statement.

In the case that you only pass the number 3, or pass it before the String you will get an error again because Scanner will first look for a String and will find an int instead.

What you can do is replace:

String checkHEmp = userChoice.next();

with

String checkHEmp = userChoice.nextLine();

and either remove

int checkHempID = userChoice.nextInt();`

or if you want to keep it you have to enter the name first , press Enter to go to a new line, enter the number and press enter again.

You also need to change

if (checkHempID == 3 || checkHEmp == "JillJones")

to

if (checkHempID == 3 || checkHEmp.equals("JillJones"));

Another thing you can do is :

Scanner userChoice = new Scanner(System.in);
System.out.println("Enter employee ID or full name");
String checkHEmp = "";
int checkHempID = 0;
if(userChoice.hasNextInt())
    checkHempID = userChoice.nextInt();
else 
    checkHEmp += userChoice.nextLine();
if (checkHempID == 3|| checkHEmp.equals("Jill Jones")) {
    //do whatever
}

This way you need to EITHER enter 3 or "Jill Jones"

Hope this helps.

takendarkk
  • 3,347
  • 8
  • 25
  • 37
gkrls
  • 2,618
  • 2
  • 15
  • 29
  • This code is exactly what I needed. So is userChoice.hasNextInt() supposed to test if what the user enters is an integer? – SeverelyConfused Aug 10 '14 at 23:16
  • It will store the next `int` it finds to the variable you assign it. If it finds a `String` instead you are in trouble. If you don't know know what comes next then you need some logic to handle it. Something like what i did on the last part – gkrls Aug 10 '14 at 23:18