Scanner.next()與Scanner.nextLine()的區別


1、next()方法在遇到有效字符前所遇到的空格、tab鍵、enter鍵都不能當作結束符,next()方法會自動將其去掉,只有當next()方法遇到有效字符之后,next()方法才將其后輸入的空格鍵、Tab鍵或Enter鍵等視為分隔符或結束符,所以next()不能得到帶有空格的字符串,只能得到部分字符串(空格前面的)。

2、nextLine()方法的結束符是Enter鍵,即nextLine()方法返回的是Enter鍵之前的所有字符串,所以nextLine()方法可以獲取到帶有空格的字符串。

package cn.laojiu.demo11;

import java.util.Scanner;
/*
 * 測試用例:
 * 輸入wei chen
 * 測試next()接收到的字符串,字符串長度
 * 測試nextLine()接收到的字符串,字符串長度
 */
public class nextOfnextLine {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("輸入:");
        String s = sc.next();
        System.out.println("next()方法接收到的字符:"+s+"\t字符長度:"+s.length());
        //如果第一次輸入的字符串帶有空格,next()接收到的是空格前面的,空格以及空格后面的由nextLine()接收了
        String c = sc.nextLine();    
        System.out.println("nextLine()方法接收到的字符:"+c+"\t字符長度:"+c.length());
    }
}

 

結果:

 
         

輸入:
wei chen
next()方法接收到的字符:wei 字符長度:3        //next()方法遇到空格鍵相當於遇到了結束符。
nextLine()方法接收到的字符: chen 字符長度:5    //nextLine()方法將next()方法接收完剩下的都接收了

 
        

 

nextInt()、nextDouble()方法與next()方法相同,一定要讀取到有效字符以后才可以結束輸入。

package cn.chengdu.zizengjian;

import java.util.Scanner;

public class Demo4 {

    public static void main(String[] args) {
        int num = scanner().nextInt();
        System.out.println("nextInt方法:"+num);
        double dnum = scanner().nextDouble();
        System.out.println("nextDouble方法:"+dnum);
    }
    public static Scanner scanner() {
        System.out.println("請輸入字符");
        Scanner input = new Scanner(System.in);
        return input;
    }

}

結果:

請輸入字符
     1               1 
nextInt方法:1
請輸入字符
       1  1
nextDouble方法:1.0

 

3、當使用next()之后接着使用nextLine(),中間需要使用nextLine()用來接收next()或者nextInt()等過濾的回車、tab、空格。這樣才能讓下面的nextLine()生效,否則它就接收了enter、tab、空格等,導致用戶沒有輸入就結束了。

 1 package cn.chengdu.zizengjian;
 2 
 3 import java.util.Scanner;
 4 
 5 
 6 /*
 7  * 錯誤示例:
 8  *         當next()下面接着使用nextLine()時,中間沒有使用nextLine()接收next()接收完后的余下字符,會導致用戶沒有進行余下的輸入直接結束程序
 9  */
10 
11 
12 public class Demo6 {
13     public static void main(String[] args) {
14         System.out.println("請輸入字符串A");
15         Scanner input = new Scanner(System.in);
16         String A = input.next();        //當next()遇到有效字符后再遇到空格、tab鍵、enter鍵就結束了接收
17         System.out.println("A接收到的輸入:"+A);
18         System.out.println("請輸入字符串C");
19         String C = input.nextLine();    //nextLine()由於沒有遇到enter鍵,繼續接收余下的字符,當遇到2后面的enter鍵時才結束接收
20         System.out.println("C接收到的輸入:"+C);
21     }
22 
23 }

結果:
請輸入字符串A
1   2
A接收到的輸入:1
請輸入字符串C
C接收到的輸入:   2

 

正確示例:

 1 package cn.chengdu.zizengjian;
 2 
 3 import java.util.Scanner;
 4 
 5 /*
 6  * 正確示例:
 7  *         在next()方法與nextLine()方法之間再調用一次nextLine()方法接收next()方法接受完后剩下來的殘兵敗將!
 8  */
 9 
10 public class Demo7 {
11 
12     public static void main(String[] args) {
13         Scanner input = new Scanner(System.in);
14         System.out.println("請輸入A:");
15         String A = input.next();
16         System.out.println("A接收到的輸入:"+A);
17         input.nextLine();        //接收A的殘兵敗將。
18         System.out.println("請輸入B:");
19         String B = input.nextLine();
20         System.out.println("B接收到的輸入:"+B);
21 
22     }
23 
24 }

結果:
請輸入A:
12 23 23
A接收到的輸入:12
請輸入B:
此處已經沒有接收到A的殘兵敗將了
B接收到的輸入:此處已經沒有接收到A的殘兵敗將了

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM