Table of Contents
scan.next()与scan.nextline()函数的使用及区别
Scanner类的next()和nextLine()方法
next()方法
nextLine()方法
Home Java javaTutorial How to use the scan.next() and scan.nextline() functions in java

How to use the scan.next() and scan.nextline() functions in java

Apr 28, 2023 pm 06:07 PM
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');
    		
    	}
    
    }
    Copy after login

    测试结果

    也验证了我的猜想

    How to use the scan.next() and scan.nextline() functions in java

    注意:

    自省,也希望能对大家有所帮助,少走弯路。

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

    运行结果截图:

    How to use the scan.next() and scan.nextline() functions in java

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

    运行结果截图:

    How to use the scan.next() and scan.nextline() functions in java

    两个方法一起用可能会出错:

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

    运行结果截图:

    How to use the scan.next() and scan.nextline() functions in java

    这时程序已结束运行,不能再输入。原因是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);
    	}
    }
    Copy after login

    运行结果截图:

    How to use the scan.next() and scan.nextline() functions in java

    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!

    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)
    2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Repo: How To Revive Teammates
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Hello Kitty Island Adventure: How To Get Giant Seeds
    3 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)

    Square Root in Java Square Root in Java Aug 30, 2024 pm 04:26 PM

    Guide to Square Root in Java. Here we discuss how Square Root works in Java with example and its code implementation respectively.

    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.

    Armstrong Number in Java Armstrong Number in Java Aug 30, 2024 pm 04:26 PM

    Guide to the Armstrong Number in Java. Here we discuss an introduction to Armstrong's number in java along with some of the code.

    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

    See all articles