How to use the scan.next() and scan.nextline() functions in java
scan.next()与scan.nextline()函数的使用及区别
今天在做牛客网编程练习题“length of last word”时,当编写实现代码时,使用split()函数对输入的字符串进行按空格符分割,确遇到了”奇葩“的问题,每次只能得到第一个字符串。
开始以为是split()函数用错了,查了资料确定无误后,觉得应该是输入的有问题。
于是进行了下面的实验:
import java.util.Scanner; public class Solution { public static void main(String[] args) { String s_next = ""; String s_nextLine = ""; int count_next = 0; // 计数 int count_nextLine = 0; // 计数 Scanner scan = new Scanner(System.in); System.out.println("请输入第一个字符串:"); s_nextLine = scan.nextLine(); // 此处使用nextLine(),便于对比 System.out.println("请输入第二个字符串:"); s_next = scan.next(); // 第一次使用的next(); scan.close(); String [] split_next = s_next.split("\\s+"); String [] split_nextLine = s_nextLine.split("\\s+"); for(String s : split_next) System.out.println("子串next: "+ count_next++ +": "+ s + " 长度: " + s.length()+ '\n'); for(String s : split_nextLine) System.out.println("子串nextLine: "+ count_nextLine++ +": "+ s + " 长度: " + s.length()+ '\n'); } }
测试结果
也验证了我的猜想
注意:
自省,也希望能对大家有所帮助,少走弯路。
用 Scanner 实现字符串的输入有两种方法,一种是next(),一种nextLine();
next() 一定要读取到有效字符后才可以结束输入,对输入有效字符之前遇到的空格键、Tab键或Enter键等结束符,next() 方法会自动将其去掉,只有在输入有效字符之后,next()方法才将其后输入的空格键、Tab键或Enter键等视为分隔符或结束符。
nextLine()方法的结束符只是Enter键。
简言之,next方法不能得到带空格的字符串,而nextLine()方法返回的是Enter键之前的所有字符,因此出现了上面测试样例的结果。(ps.一定要注意!)
Scanner类的next()和nextLine()方法
java的Scanner类可以用来接收键盘输入的数据。next()和nextLine()方法用来接收字符串,next()方法接收字符串时遇到空格或回车结束输入,而nextLine()方法可以接收空格,最后输入回车才结束。下面用实例演示
两者的区别:
next()方法
package scanner; import java.util.Scanner; public class Scan { public static void main(String[] args) { String a,b; Scanner sc=new Scanner(System.in); System.out.println("next()方法接收字符串:"); a=sc.next(); System.out.println(a); } }
运行结果截图:
nextLine()方法
package scanner; import java.util.Scanner; public class Scan { public static void main(String[] args) { String a,b; Scanner sc=new Scanner(System.in); System.out.println("nextLine()方法接收字符串:"); b=sc.nextLine(); System.out.println(b); } }
运行结果截图:
两个方法一起用可能会出错:
package scanner; import java.util.Scanner; public class Scan { public static void main(String[] args) { String a,b; Scanner sc=new Scanner(System.in); System.out.println("next()方法接收字符串:"); a=sc.next(); System.out.println(a); System.out.println("nextLine()方法接收字符串:"); b=sc.nextLine(); System.out.println(b); } }
运行结果截图:
这时程序已结束运行,不能再输入。原因是next()方法遇到回车结束输入,却把最后的回车符留给了nextLine(),nextLine()方法接收了一个空字符串。
解决方法是next()方法后面再加一个nextLine()用来接收回车符,代码如下:
package scanner; import java.util.Scanner; public class Scan { public static void main(String[] args) { String a,b; Scanner sc=new Scanner(System.in); System.out.println("next()方法接收字符串:"); a=sc.next(); System.out.println(a); a=sc.nextLine();//接收回车符 System.out.println("nextLine()方法接收字符串:"); b=sc.nextLine(); System.out.println(b); } }
运行结果截图:
The above is the detailed content of How to use the scan.next() and scan.nextline() functions in java. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

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

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.
