scanner

Java中Scanner用法總結

Scanner類簡介

Java 5添加了java.util.Scanner類,這是一個用於掃描輸入文本的新的實用程序。它是以前的StringTokenizer和Matcher類之間的某種結合。由於任何數據都必須通過同一模式的捕獲組檢索或通過使用一個索引來檢索文本的各個部分。於是可以結合使用正則表達式和從輸入流中檢索特定類型數據項的方法。這樣,除了能使用正則表達式之外,Scanner類還可以任意地對字符串和基本類型(如int和double)的數據進行分析。藉助於Scanner,可以針對任何要處理的文本內容編寫自定義的語法分析器。

關於nextInt()、next()和nextLine()的理解

nextInt(): 只讀取數值,剩下”\n”還沒有讀取,並將光標留在本行。

next(): 只讀空格之前的數據,並且光標指向本行
next()方法遇見第一個有效字符(非空格,非換行符)時,開始掃描,當遇見第一個分隔符或結束符(空格或換行符)時,結束掃描,獲取掃描到的內容,即獲得第一個掃描到的不含空格、換行符的單個字符串。

nextLine():可以掃描到一行內容並作爲一個字符串而被獲取到。

public class NextTest{  
    public static void main(String[] args) {  
        String s1,s2;  
        Scanner sc=new Scanner(System.in);  
        System.out.print("請輸入第一個字符串:");  
        s1=sc.nextLine();  
        System.out.print("請輸入第二個字符串:");  
        s2=sc.next();  
        System.out.println("輸入的字符串是:"+s1+" "+s2);  
    }  
}  

結果:

請輸入第一個字符串:home
請輸入第二個字符串:work
輸入的字符串是:home work

把上面的程序修改一下:

s1=sc.next();  
s2=sc.nextLine();  

運行結果:

請輸入第一個字符串:home
請輸入第二個字符串:輸入的字符串是:home

可以看到,nextLine()自動讀取了被next()去掉的Enter作爲他的結束符,所以沒辦法給s2從鍵盤輸入值。經過驗證,我發現其他的next的方法,如double nextDouble() , float nextFloat() , int nextInt() 等與nextLine()連用時都存在這個問題,解決的辦法是:在每一個 next()、nextDouble() 、 nextFloat()、nextInt() 等語句之後加一個nextLine()語句,將被next()去掉的Enter結束符過濾掉

public class NextTest{  
    public static void main(String[] args) {  
        String s1,s2;  
        Scanner sc=new Scanner(System.in);  
        System.out.print("請輸入第一個字符串:");  
        s1=sc.next();  
        sc.nextLine();
        System.out.print("請輸入第二個字符串:");  
        s2=sc.nextLine();  
        System.out.println("輸入的字符串是:"+s1+" "+s2);  
    }  
}  

運行結果:

請輸入第一個字符串:home
請輸入第二個字符串:work
輸入的字符串是:home work

循環輸入多組測試用例

一個while就是一個測試用例

    public static void main(String[] args){
        Scanner in = new Scanner(System.in);

        // 一個while就是一個測試用例
        while(in.hasNext()){
            int n = in.nextInt(); // 該測試用例後續接收的參數個數
            long[] array = new long[n];
            String[] arrayStr = new String[n];
            for(int i=0; i<n; i++){
                arrayStr[i] = in.next();
            }
            for(int i=0; i<n; i++){
                array[i] = in.nextLong();// 取下一個元素轉換成long類型
            }

            System.out.println(Arrays.toString(array)+" "+ Arrays.toString(arrayStr));
        }
    }

一個與容器結合的綜合例子:

import java.util.Scanner;    
public class Main {    
    public static void main(String[] args) {    
        Scanner in = new Scanner(System.in);    
        while (in.hasNext()) {    
            int n = in.nextInt();   
        /* nextLine()是掃描器執行當前行,並返回跳過的輸入信息,特別需要注意!!! 

            如果沒有該行,則執行第一個in.nextLine()命令時的返回值是int n = in.nextInt()的值*/   
            in.nextLine();  
        HashSet<String> set = new HashSet<String>();  
        for (int i = 0; i < n; i++) {   
        String line =   

        in.nextLine();   
        String[] arr = line.split(" ");   
        for (int j = 0; j < arr.length; j++) {   
            set.add(arr[j]);   
        }  
         }  
        System.out.println("sum:" + set.size());    

    }    
}  

輸入:
3
a b c
d e f
a b c
輸出:
6

發佈了43 篇原創文章 · 獲贊 19 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章