Q:

How to display a null message for an int in java when the user hits enter with no value

I am trying to create a validation for the user when inputting integer data for a switch statement in java. My code is:
//Checks if inputed value is number or String
    public void valsInt(String entry){
        if(in.hasNextInt()){
            setUserInputInteger(in.nextInt());
            //System.out.println("valInt = Correct");
            setIsTrue(true);
        }else{
            if(in.hasNextLine()){
                setUserInputString(in.nextLine());
                System.out.println("`"+getUserInputString()+"` IS AN INVALID ENTRY. YOU CAN ONLY ENTER DIGITS FOR THE " + entry);
                setIsTrue(false);

            }
        }

    }

What this code does is prompt the user for a integer, if it is an integer, it is set the variable"userInputInteger" to the input data and the loop would stop but if the user inputs a String, the user will receive a message saying that this is not a valid input as it is an String. I am trying to create an error message when the enter button is clicked with no values inside but the "in.hasNextInteger" can not pick up it as not being an integer so will not move into else part of if statement.
. 
Has anyone got any ideas on this
Thanks in advance

A:

One way to achieve that is:

Surround your method with a try-catch statement to avoid use that hasNextInt()
Surround your method with an if statement to verify if the entry is empty

Like this:
public void valsInt(String entry){
    if(!entry.trim().isEmpty()){
        try{
            int number = Integer.parseInt(entry);
            setIsTrue(true);
        }catch(NumberFormatException e){
            System.out.println("`"+getUserInputString()+"` IS AN INVALID ENTRY. YOU CAN ONLY ENTER DIGITS FOR THE " + entry);
            setIsTrue(false);
        }
    }else{
        System.out.println("THE ENTRY IS EMPTY");
    }
}

