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!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.