Java 输入为空时如何返回语句?

Java 输入为空时如何返回语句?,java,java.util.scanner,Java,Java.util.scanner,我试图让我的代码在输入为空时返回语句。这是我目前的代码 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;

我试图让我的代码在输入为空时返回语句。这是我目前的代码

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;
}
我希望它说“没有输入员工信息”,并返回false。
我该怎么做呢?谢谢。

修剪后它不能有空间,所以应该是“”


修剪后它不能有空间,所以它应该是“”


我建议这样做:

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;
}
public boolean inputLastName(){
    System.out.println("Enter New Employee Information");
    System.out.print("Enter Employee Last Name: ");
    lastName = sc.nextLine();

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

我认为,仅仅通过测试字符串是否为空,这就更简单了。通过只使用一个return语句,这也使它更简洁。

我建议这样做:

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;
}
public boolean inputLastName(){
    System.out.println("Enter New Employee Information");
    System.out.print("Enter Employee Last Name: ");
    lastName = sc.nextLine();

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

我认为,仅仅通过测试字符串是否为空,这就更简单了。这还可以通过只使用一个return语句使其更简洁。

我建议您添加一个StringBuffer或StringBuilder(而不是String,因为它是不可变的)作为方法的参数,并在其中放入适当的文本

编辑:
抱歉,我误读了这个问题,@Benjamin Lawry的答案是正确的

我建议您添加一个StringBuffer或StringBuilder(而不是String,因为它是不可变的)作为方法的参数,并在其中放入适当的文本

编辑:
抱歉,我误读了这个问题,@Benjamin Lawry的答案是正确的

我想,问题是System.out没有时间继续输出,所以您可以添加System.out.flush()以使其打印缓冲区,如下所示:

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;
}
public boolean inputLastName(){
    System.out.println("Enter New Employee Information");
    System.out.print("Enter Employee Last Name: ");
    lastName = sc.nextLine();

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

我想,问题是System.out没有时间继续输出,所以可以添加System.out.flush()使其打印缓冲区,如下所示:

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;
}
public boolean inputLastName(){
    System.out.println("Enter New Employee Information");
    System.out.print("Enter Employee Last Name: ");
    lastName = sc.nextLine();

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

谢谢,试过了,但没什么不同。它仍然像往常一样继续。@JohnnyB嗨,对不起,我刚刚编辑了这篇文章。我把sc.next()改为sc.nextLine(),现在它应该适合你了。@JohnnyB没问题,很乐意帮忙。请记住,你可以通过接受答案来帮助其他人在将来查看此帖子,谢谢!谢谢,试过了,但没什么不同。它仍然像往常一样继续。@JohnnyB嗨,对不起,我刚刚编辑了这篇文章。我把sc.next()改为sc.nextLine(),现在它应该适合你了。@JohnnyB没问题,很乐意帮忙。请记住,你可以通过接受答案来帮助其他人在将来查看此帖子,谢谢!我也试过了,但没有什么不同。@JohnnyB运行此代码后得到的输出是什么?输入新员工信息输入员工姓氏:然后当我按Enter nothing Happens时,我也试过了,而且没有发生任何变化。@JohnnyB运行此代码后得到的输出是什么?输入新员工信息输入员工姓氏:然后当我点击输入nothing Happers时,使用
org.apache.commons.lang.StringUtils
use
org.apache.commons.lang.StringUtils