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

》概述

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

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

》舉例說明

  鑑於以上兩種方法的只要區別,同窗們必定要注意next()方法和nextLine()方法的連用,下面舉個例子來講明:dom

 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從鍵盤輸入值。ide

  通過驗證,其餘的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 }
隨機驗證碼的匹配
相關文章
相關標籤/搜索