Java 尝试从输入中删除空新行(使用扫描仪)

Java 尝试从输入中删除空新行(使用扫描仪),java,input,newline,java.util.scanner,Java,Input,Newline,Java.util.scanner,作为家庭作业的一部分,我需要制作一个需要用户输入的程序。现在我坚持使用控制台,但是我希望避免由于换行符而导致崩溃 这是一个测试,它的行为类似于我试图做的,甚至由于一个换行符而崩溃 public void testRead() { Scanner input = new Scanner(System.in); String s1 = "", s2 = "", s3 = "", s4 = "", s5 = ""; while (s1 == "" || s1 =="\n"

作为家庭作业的一部分,我需要制作一个需要用户输入的程序。现在我坚持使用控制台,但是我希望避免由于换行符而导致崩溃

这是一个测试,它的行为类似于我试图做的,甚至由于一个换行符而崩溃

public void testRead() {

    Scanner input = new Scanner(System.in);

    String s1 = "", s2 = "", s3 = "", s4 = "", s5 = "";

    while (s1 == "" || s1 =="\n") 
        if (input.hasNext()) {
            s1 = input.nextLine();
        }
    while (s2 == "" || s2 =="\n") 
        if (input.hasNext()) {
            s2 = input.nextLine();
        }
    while (s3 == "" || s3 == "\n") 
        if (input.hasNext()) {
            s3 = input.nextLine();
        }
    while (s4 == "" || s4 == "\n") 
        if (input.hasNext()) {
            s4 = input.nextLine();
        }
    while (s5 == "" || s5 == "\n") 
        if (input.hasNext()) {
            s5 = input.nextLine();
        }
        // Here is why it might crash
    if (input.hasNextInt()) // even though it should not pass this if
            // However the if is not the issue. 
            // This input may even be in another function
           int crash = input.nextLine();

    System.out.println("s1: " + s1);
    System.out.println("s2: " + s2);
    System.out.println("s3: " + s3);
    System.out.println("s4: " + s4);
    System.out.println("s5: " + s5);
}
}

我希望
while
语句能够解决这个问题,但事实并非如此

我可以解决崩溃问题,但这并不能解决问题,因为我仍然有空字符串等等

示例

This is the first string
This is the second string
                                          <- pressed enter again, by mistake or not
This is the third string
This is the fourth string
//现在崩溃。同样,崩溃是可以避免的,但我仍然有一个问题,字符串3没有被读取或包含。。。。我不想要的东西


有没有简单的方法来解决这个问题?如果没有简单的方法,我宁愿忽略它,尽快完成我的家庭作业,但我仍然想知道详细的答案,以供将来参考。

您不能使用
==
来实现字符串内容相等,请尝试以下方法:

 while (s1.equals("") || s1.equals("\n")) 
        if (input.hasNext()) {
            s1 = input.nextLine();
        }
    while (s2.equals("") || s2.equals("\n")) 
        if (input.hasNext()) {
            s2 = input.nextLine();
        }
    while (s3.equals("") || s3.equals("\n")) 
        if (input.hasNext()) {
            s3 = input.nextLine();
        }
    while (s4.equals("") || s4.equals("\n")) 
        if (input.hasNext()) {
            s4 = input.nextLine();
        }
    while (s5.equals("") || s5.equals("\n")) 
        if (input.hasNext()) {
            s5 = input.nextLine();
        }
 while (s1.equals("") || s1.equals("\n")) 
        if (input.hasNext()) {
            s1 = input.nextLine();
        }
    while (s2.equals("") || s2.equals("\n")) 
        if (input.hasNext()) {
            s2 = input.nextLine();
        }
    while (s3.equals("") || s3.equals("\n")) 
        if (input.hasNext()) {
            s3 = input.nextLine();
        }
    while (s4.equals("") || s4.equals("\n")) 
        if (input.hasNext()) {
            s4 = input.nextLine();
        }
    while (s5.equals("") || s5.equals("\n")) 
        if (input.hasNext()) {
            s5 = input.nextLine();
        }