最近在做OJ類問題的時候,經(jīng)常由于Scanner的使用造成一些細(xì)節(jié)問題導(dǎo)致程序不通過(最慘的就是網(wǎng)易筆試,由于sc死循環(huán)了也沒發(fā)現(xiàn),導(dǎo)致AC代碼也不能通過。。。),因此對Scanner進(jìn)行了一些總結(jié)整理。
Scanner類簡介
Java 5添加了java.util.Scanner類,這是一個用于掃描輸入文本的新的實用程序。
它是以前的StringTokenizer和Matcher類之間的某種結(jié)合。由于任何數(shù)據(jù)都必須通過同一模式的捕獲組檢索或通過使用一個索引來檢索文本的各個部分。
于是可以結(jié)合使用正則表達(dá)式和從輸入流中檢索特定類型數(shù)據(jù)項的方法。這樣,除了能使用正則表達(dá)式之外,Scanner類還可以任意地對字符串和基本類型(如int和double)的數(shù)據(jù)進(jìn)行分析。
借助于Scanner,可以針對任何要處理的文本內(nèi)容編寫自定義的語法分析器。
關(guān)于nextInt()、next()和nextLine()的理解
nextInt(): it only reads the int value, nextInt() places the cursor(光標(biāo)) in the same line after reading the input.(nextInt()只讀取數(shù)值,剩下"\n"還沒有讀取,并將cursor放在本行中)
next(): read the input only till the space. It can't read two words separated by space. Also, next() places the cursor in the same line after reading the input.(next()只讀空格之前的數(shù)據(jù),并且cursor指向本行)
next() 方法遇見第一個有效字符(非空格,非換行符)時,開始掃描,當(dāng)遇見第一個分隔符或結(jié)束符(空格或換行符)時,結(jié)束掃描,獲取掃描到的內(nèi)容,即獲得第一個掃描到的不含空格、換行符的單個字符串。
nextLine(): reads input including space between the words (that is, it reads till the end of line \n). Once the input is read, nextLine() positions the cursor in the next line.
nextLine()時,則可以掃描到一行內(nèi)容并作為一個字符串而被獲取到。
1
2
3
4
5
6
7
8
9
10
11
|
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); } } |
結(jié)果:
請輸入第一個字符串:home
請輸入第二個字符串:work
輸入的字符串是:home work
把上面的程序修改一下:
1
2
|
s1=sc.next(); s2=sc.nextLine(); |
運行結(jié)果:
請輸入第一個字符串:home
請輸入第二個字符串:輸入的字符串是:home
可以看到,nextLine()自動讀取了被next()去掉的Enter作為他的結(jié)束符,所以沒辦法給s2從鍵盤輸入值。
經(jīng)過驗證,我發(fā)現(xiàn)其他的next的方法,如double nextDouble() , float nextFloat() , int nextInt() 等與nextLine()連用時都存在這個問題,解決的辦法是:在每一個 next()、nextDouble() 、 nextFloat()、nextInt() 等語句之后加一個nextLine()語句,將被next()去掉的Enter結(jié)束符過濾掉。
1
2
3
4
5
6
7
8
9
10
11
12
|
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); } } |
運行結(jié)果:
請輸入第一個字符串:home
請輸入第二個字符串:work
輸入的字符串是:home work
循環(huán)輸入多組測試用例
一個while就是一個測試用例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public static void main(String[] args){ Scanner in = new Scanner(System.in); // 一個while就是一個測試用例 while (in.hasNext()){ int n = in.nextInt(); // 該測試用例后續(xù)接收的參數(shù)個數(shù) 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(); // 取下一個元素轉(zhuǎn)換成long類型 } System.out.println(Arrays.toString(array)+ " " + Arrays.toString(arrayStr)); } } |
一個與容器結(jié)合的綜合例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
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()是掃描器執(zhí)行當(dāng)前行,并返回跳過的輸入信息,特別需要注意!!! 如果沒有該行,則執(zhí)行第一個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
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/Megustas_JJC/article/details/68960433