java中nextLine()和next()的區別


》概述

  在實現字符窗口的輸入時,我個人更喜歡選擇使用掃描器Scanner,它操作起來比較簡單。我發現用Scanner實現字符串的輸入有兩種方法,一種是next(),一種nextLine(),但是這兩種方法究竟有什么區別呢?

  》》1.首先,next()一定要讀取到有效字符后才可以結束輸入,對輸入有效字符之前遇到的空格鍵、Tab鍵或Enter鍵等結束符,next()方法會自動將其去掉,只有在輸入有效字符之后,next()方法才將其后輸入的空格鍵、Tab鍵或Enter鍵等視為分隔符或結束符。簡單地說,next()查找並返回來自此掃描器的下一個完整標記。完整標記的前后是與分隔模式匹配的輸入信息,所以next方法不能得到帶空格的字符串;而nextLine()方法的結束符只是Enter鍵,即nextLine()方法返回的是Enter鍵之前的所有字符,它是可以得到帶空格的字符串的。

》舉例說明

  鑒於以上兩種方法的只要區別,同學們一定要注意next()方法和nextLine()方法的連用,下面舉個例子來說明:

 1 import java.util.Scanner;
 2 
 3 public class Test_next {
 4     public static void main(String[] args) {
 5         String s1,s2;
 6         Scanner scanner=new Scanner(System.in);
 7         System.out.println("請輸入第一個字符串:"); 
 8         s1=scanner.next();
 9         System.out.println("請輸入第二個字符串:");
10         s2=scanner.nextLine();
11         System.out.println("輸入的字符串是:"+s1+s2);
12     }
13 
14 }
測試代碼
1 請輸入第一個字符串:
2 home 
3 請輸入第二個字符串:
4 輸入的字符串是:home 
運行結果

》結論

  nextLine()自動讀取了被next()去掉的Enter作為他的結束符,所以沒辦法給s2從鍵盤輸入值。

  經過驗證,其他的next的方法,如double nextDouble()  , float nextFloat() , int nextInt() 等與nextLine()連用時都存在這個問題。

》解決的辦法

在每一個 next()、nextDouble()  、 www.gzlij.com()、nextFloat()、nextInt() 等語句之后加一個nextLine()語句,將被next()去掉的Enter結束符過濾掉

 1 import java.util.Scanner;
 2 
 3 public class Test_next {
 4     public static void main(String[] args) {
 5         String s1,s2;
 6         Scanner scanner=new Scanner(System.in);
 7         System.out.println("請輸入第一個字符串:"); 
 8         s1=scanner.next();
 9         scanner.nextLine();
10         System.out.println("請輸入第二個字符串:");
11         s2=scanner.nextLine();
12         System.out.println("輸入的字符串是:"+s1+s2);
13     }
14 
15 }
測試代碼
1 請輸入第一個字符串:
2 home 
3 請輸入第二個字符串:
4 work
5 輸入的字符串是:homework
運行結果

 》 應用舉例

 1 package atest.dak.com;
 2 
 3 import java.util.Arrays;
 4 import java.util.Scanner;
 5 
 6 /*
 7  * 隨機生成指定位數的驗證碼
 8  * 提醒用戶輸入
 9  * 判斷用戶輸入是否正確
10  */
11 public class Test{
12     public static void main(String[] args){
13         Scanner scan = new Scanner(System.in);
14         
15         System.out.print("請輸入隨機產生的驗證碼的個數:");
16         int number = scan.nextInt();
17         
18         char[] arr_of_random = randome_num(number);  //獲取隨機驗證碼
19         System.out.println(Arrays.toString(arr_of_random)); //打印隨機驗證碼
20         
21         System.out.print("請輸入驗證碼:");
22         
23         /*
24          * 注意:由於之前進行輸入的時候是用回車作為結束的,所以如果用nextline()的話就會
25          *         將上一次的結束的回車作為這次的輸入,又因為nextline()的結束也是回車,所以
26          *         在這里通過nextline()作為輸入函數是不會得到任何輸入值的;因此我們用next()
27          *         作為這里的輸入函數可以有效的避免這種問題。
28          */
29         String answer = scan.next();  //獲取輸入字符串
30         char[] arr_of_answer = answer.toCharArray(); //將輸入字符串轉化成字符數組
31         System.out.println(Arrays.toString(arr_of_answer)); //打印輸入字符數組
32         
33         int[] result = match(arr_of_answer, arr_of_random);
34         System.out.println(Arrays.toString(result));
35         
36     }
37     
38 //    產生所有小寫字母
39     private static void f1(){
40         for(int i = 97; i < 97 + 26; i++){
41             System.out.print("\'"+(char)i+"\'"+",");
42         }
43     }
44     
45 //    產生所有大寫字母
46     private static void f2(){
47         for(int i = 65; i < 65 + 26; i++){
48             System.out.print("\'"+(char)i+"\'"+",");
49         }
50     }
51     
52 //    隨機產生指定個數的驗證碼
53     private static char[] randome_num(int figure){
54         char[] cha01 = {'a','b','c','d','e','f','g','h',
55                 'i','j','k','l','m','n','o','p','q','r','s',
56                 't','u','v','w','x','y','z','A','B','C','D',
57                 'E','F','G','H','I','J','K','L','M','N','O',
58                 'P','Q','R','S','T','U','V','W','X','Y','Z'};
59         boolean[] used = new boolean[cha01.length];
60         char[] cha02 = new char[figure];
61         for(int i = 0; i < cha02.length; i++){
62             int j;
63             
64             //進行去重復處理
65             do{
66                 j = (int)(Math.random()*(cha01.length));
67             }while(used[j]);
68             
69             cha02[i] = cha01[j];
70             used[j] = true;
71         }
72         return cha02;
73     }
74 
75 //    對隨機驗證碼和輸入的驗證碼進行匹配
76     private static int[] match(char[] answer, char[] random){
77         int[] result = new int[2];
78         for(int i = 0; i < answer.length; i++){
79             for(int j = 0; j < random.length; j++){
80                 if(random[j] == answer[i]){
81                     result[0]++;
82                     if(j == i){
83                         result[1]++;
84                     }
85                 }
86             }
87         }
88         return result;
89     }
90 }
隨機驗證碼的匹配

 


免責聲明!

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



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