日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

[转载] Java中Scanner用法总结

發(fā)布時間:2025/3/11 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [转载] Java中Scanner用法总结 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

參考鏈接: Java之Scanner類

最近在做OJ類問題的時候,經(jīng)常由于Scanner的使用造成一些細節(jié)問題導致程序不通過(最慘的就是網(wǎng)易筆試,由于sc死循環(huán)了也沒發(fā)現(xiàn),導致AC代碼也不能通過。。。),因此對Scanner進行了一些總結(jié)整理。(我的github:https://github.com/MonkeyJJC?tab=repositories)?

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

關(guān)于nextInt()、next()和nextLine()的理解?

nextInt(): it only reads the int value, nextInt() places the cursor(光標) 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() 方法遇見第一個有效字符(非空格,非換行符)時,開始掃描,當遇見第一個分隔符或結(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)容并作為一個字符串而被獲取到。?

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

?

把上面的程序修改一下:?

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é)束符過濾掉。?

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就是一個測試用例?

? ? 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é)合的綜合例子:?

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í)行當前行,并返回跳過的輸入信息,特別需要注意!!!?

?

? ? ? ? ? ? 如果沒有該行,則執(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

總結(jié)

以上是生活随笔為你收集整理的[转载] Java中Scanner用法总结的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。