-1

Is it possible to implement the commented lines in order to distinguish between an Integer and a String when a user inputs said value into the console? The code works with the rest of my program, but I want the user to be able to input the Employee ID (an int) in addition to name and/or address.

public void query(){

    String newValue;

    System.out.println("Please enter the name, address, or employee "
            + "ID you are looking for");
    Scanner input = new Scanner(System.in);
    //if (Integer.parseInt(input);
        //newValue = input.nextInt();
    //else
    newValue = input.nextLine();
    input.close();

    if (name.equals(newValue) | address.equals(newValue) )
        System.out.println(toString());
    else System.out.println("There is no employee with that name, address, or ID");


}
  • Quite a lot of addresses start with an integer value. You probably want to start with an input where the user tells you which piece of information they're going to provide (i.e., "enter 1 for name, 2 for address, 3 for employee id...), then you'll know what kind of data to expect next. Otherwise, you could just store your employee IDs as strings and bypass the problem altogether. – Bill the Lizard May 29 '17 at 22:03
  • Looked for similar posts, didn't see anything. I will look at your suggestion. – user3062174 May 29 '17 at 22:33
  • Bill the Lizard. I thought of using the "enter 1 for name" concept, but I was hoping to find a simpler solution. And you're right, I could just make the ID a String. – user3062174 May 29 '17 at 22:36
  • cricket_007 was correct. Question has already been asked and answered. Thank you! – user3062174 May 30 '17 at 00:06

1 Answers1

0

Yes, you can do that using isNumeric and isAlpha methods of StringUtils class from Apache Commons library.

Dmitriy
  • 86
  • 1
  • 5