Home >Java >javaTutorial >How to solve the problem of ending the blank line in Java

How to solve the problem of ending the blank line in Java

PHPz
PHPzforward
2023-05-15 13:10:061492browse

javaEnter a blank line to end

The last two written tests have been troubled by this problem

How to stop input after entering a blank line, I have tried various methods, the following is how to achieve this Purpose code:

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(true){//第一处
String s = in.nextLine();
if(s.equals(""))//第二处
break;
System.out.println(s);
}
System.out.println("Over Input");
}

The first judgment condition of the above code can be replaced by in.hasNextLine(), but it cannot be in.hasNext(). The second judgment condition can be s.isEmpty(), It can also be s.length == 0.

I searched for information and found:

next() and nextLine have the following differences:

next() must read valid characters. You can end the input. The next() method will automatically remove the ending characters such as the space bar, Tab key or Enter key encountered before entering a valid character. Only after the valid character is entered, the next() method will enter the subsequent character. The space bar, Tab key or Enter key are regarded as separators or terminators.

Simply put, the next method cannot get a string with spaces.

The end character of the nextLine() method is just the Enter key, that is, the nextLine() method returns all the characters before the Enter key, and it can get a string with spaces.

Java input data, space to continue, press Enter to end the input

Normal version

can be input and output. With detailed comments

import java.util.Scanner;
public class SumDemo {
    public static void main(String[] args) {
        System.out.println("请输入两个数字,中间用空格隔开,例如5 5");
        //得到一个扫描器,用来扫描 系统的输入
        Scanner input = new Scanner(System.in);
        //申明一个临时的字符串变量temp,用来保存 扫描器读取的一行;
        String temp = input.nextLine();
        //temp字符串首先trim()一下,就是去掉两边的空白,
        //因为有的人可能输入的是 空格5空格5空格回车。.
        //所以去掉两边的空格变成 5空格5回车 就符合要求了
        //split(" ")方法表示,用空格去切割字符串,返回的结果是一个字符串数组
        String[] ss = temp.trim().split(" ");
        //从两个字符串中解析得到两个数字,并求和
        int num1 = Integer.parseInt(ss[0]);
        int num2 = Integer.parseInt(ss[1]);
        int sum = num1+num2;
        //输出结果
        System.out.println("输入的数字是"+num1+" "+num2+"两数的和是:"+sum);
        //养成良好的习惯,打开了的资源要记得关闭,我们打开了扫描器,就要关闭扫描器
        input.close();
    }
}

Upgraded version

Can repeatedly input numbers, repeatedly output results, and has an exit function,

import java.util.Scanner;
public class SumTest {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        while(true){
            System.out.println("如果输入exit,那么退出。输入两个数字,用空格隔开");
            String temp = input.nextLine();
            if(temp.trim().equals("exit")){
                break;
            }
            String[] ss = temp.trim().split(" ");
            int num1 = Integer.parseInt(ss[0]);
            int num2 = Integer.parseInt(ss[1]);
            int sum = num1+num2;
            System.out.println("输入的数字是"+num1+" "+num2+"两数的和是:"+sum);
        }
        input.close();
    }
}

The above is the detailed content of How to solve the problem of ending the blank line in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete