Table of Contents
javaEnter a blank line to end
The last two written tests have been troubled by this problem
Java input data, space to continue, press Enter to end the input
Normal version
Upgraded version
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

May 15, 2023 pm 01:10 PM
java

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");
}
Copy after login

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();
    }
}
Copy after login

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();
    }
}
Copy after login

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

See all articles