JohnnyB JohnnyB - 7 months ago 7
Java Question

How to return a statement when the input is blank?

I'm trying to get my code to return a statement when my input is blank. Here is my current code.

public boolean inputLastname(){
System.out.println("Enter New Employee Information");
System.out.print("Enter Employee Last Name: ");
lastName = sc.next();

if(lastName.trim().equals(" "))
return false;
return true;
}


I'd like it to say "No Employee Information Entered" and return false as well.
How would I go about doing this? Thanks.

Answer

I would suggest something like this:

public boolean inputLastname(){
   System.out.println("Enter New Employee Information");
   System.out.print("Enter Employee Last Name: ");
   lastName = sc.nextLine();

   Boolean returnBool = true;

   if(lastName.isEmpty()){
       returnBool = false;
       System.out.println("No Employee Information Entered");
       }
   return returnBool;
}

I think this is more straightforward just by testing whether or not the String is empty. This also makes it cleaner by only having one return statement.

Comments