本篇內容主要講解“Java中的Scanner類怎么使用”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“Java中的Scanner類怎么使用”吧!
java.util.Scanner 是 Java5 的新特征,我們可以通過 Scanner 類來獲取用戶的輸入。下面是創建 Scanner 對象的基本語法:
Scanner s = new Scanner(System.in);
演示一個最簡單的數據輸入,并通過 Scanner 類的 next() 與 nextLine() 方法獲取輸入的字符串,在讀取前我們一般需要 使用 hasNext 與 hasNextLine 判斷是否還有輸入的數據:
使用 next 方法:
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
// 從鍵盤接收數據
Scanner scan = new Scanner(System.in);
// next方式接收字符串
System.out.println("next方式接收:");
// 判斷是否還有輸入
if (scan.hasNext()) {
String str1 = scan.next();
System.out.println("輸入的數據為:" + str1);
}
scan.close();
}
}
// 執行以上程序輸出結果為:
// next方式接收:
// juejin cn
// 輸入的數據為:juejin
// Process finished with exit code 0可以看到 cn 字符串并未輸出,接下來我們看 nextLine。
使用 nextLine 方法:
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
// 從鍵盤接收數據
Scanner scan = new Scanner(System.in);
// nextLine方式接收字符串
System.out.println("nextLine方式接收:");
// 判斷是否還有輸入
if (scan.hasNextLine()) {
String str2 = scan.nextLine();
System.out.println("輸入的數據為:" + str2);
}
scan.close();
}
}
// 執行以上程序輸出結果為:
// nextLine方式接收:
// juejin cn
// 輸入的數據為:juejin cn
// Process finished with exit code 0nextLine 方法可以看到 cn 字符串輸出。
next() 與 nextLine() 區別:
next():
1、一定要讀取到有效字符后才可以結束輸入。
2、對輸入有效字符之前遇到的空白,next() 方法會自動將其去掉。
3、只有輸入有效字符后才將其后面輸入的空白作為分隔符或者結束符。
next() 不能得到帶有空格的字符串。
nextLine():
1、以Enter為結束符,也就是說 nextLine()方法返回的是輸入回車之前的所有字符。
2、可以獲得空白。
如果要輸入 int 或 float 類型的數據,在 Scanner 類中也有支持,但是在輸入之前最好先使用 hasNextXxx() 方法進行驗證,再使用 nextXxx() 來讀?。?/p>
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
// 從鍵盤接收數據
Scanner scan = new Scanner(System.in);
int i = 0;
float f = 0.0f;
System.out.print("輸入整數:");
if (scan.hasNextInt()) {
// 判斷輸入的是否是整數
i = scan.nextInt();
// 接收整數
System.out.println("整數數據:" + i);
} else {
// 輸入錯誤的信息
System.out.println("輸入的不是整數!");
}
System.out.print("輸入小數:");
if (scan.hasNextFloat()) {
// 判斷輸入的是否是小數
f = scan.nextFloat();
// 接收小數
System.out.println("小數數據:" + f);
} else {
// 輸入錯誤的信息
System.out.println("輸入的不是小數!");
}
System.out.println("關閉輸入");
scan.close();
}
}
// 執行以上程序輸出結果為:
// 輸入整數:12
// 整數數據:12
// 輸入小數:1.2
// 小數數據:1.2
// 關閉輸入
// Process finished with exit code 0以下實例我們可以輸入多個數字,并求其總和與平均數,每輸入一個數字用回車確認,通過輸入非數字來結束輸入并輸出執行結果:
import java.util.Scanner;
class RunoobTest {
public static void main(String[] args) {
System.out.println("請輸入數字:");
Scanner scan = new Scanner(System.in);
double sum = 0;
int m = 0;
while (scan.hasNextDouble()) {
double x = scan.nextDouble();
m = m + 1;
sum = sum + x;
}
System.out.println(m + "個數的和為" + sum);
System.out.println(m + "個數的平均值是" + (sum / m));
scan.close();
}
}
// 執行以上程序輸出結果為(輸入非數字來結束輸入):
// 請輸入數字:
// 12
// 23
// 15
// 21.4
// end
// 4個數的和為71.4
// 4個數的平均值是17.85Scanner 不僅能從輸入流中讀取,也能從文件中讀?。?/p>

public class Test {
public static void main(String args[]) throws IOException {
int[] arr = new int[10];
int i = 0;
// 創建 File 對象
File file = new File("/Users/wanggang/Desktop/JavaTest/Hello.txt");
// 創建 Scanner 對象
Scanner sc = new Scanner(file);
// 判斷是否還有輸入
while (sc.hasNextLine()){
// 將輸入接收,否則會阻塞
sc.nextLine();
//判斷輸入的是否是整數
while(sc.hasNextInt()) {
// 將整數存入數組
arr[i] = sc.nextInt();
I++;
}
}
// 關閉 Scanner
sc.close();
// 輸出讀取的整數個數
System.out.printf("讀取了 %d 個數\n",i);
// 輸出所有讀取的整數
for(int j = 0; j < i; j++) {
System.out.print(arr[j] + ",");
}
}
}
// 執行以上程序輸出結果為:
// 讀取了 5 個數
// 1,2,3,4,5,到此,相信大家對“Java中的Scanner類怎么使用”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。