/*
* Scanner 類介紹
* Scanner 對(duì)象的構(gòu)造方式:
* ——使用字符串直接構(gòu)造Scanenr對(duì)象
* ——使用一個(gè)File對(duì)象來構(gòu)造Scanenr對(duì)象
* ——從標(biāo)準(zhǔn)輸入構(gòu)造一個(gè)Scanner對(duì)象
[從標(biāo)準(zhǔn)輸入讀取字符串的時(shí)候,也可以使用重定向?qū)⒁粋€(gè)文件綁定到System.in中]
*
* Scanner中的常用方法
* (1)hasNext() 是否還有未讀取的字符
* (2)next() 下一個(gè)單詞暖释,默認(rèn)空格分隔
* (3)nextLine() 按行讀取作郭,下一行
* (4)nextInt/long 讀取一個(gè)整形/長整形字符串
* (5)nextFlaot/Double 讀取一個(gè)單精度/雙精度字符串
*/
package com.michael.lin;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;
public class Demo02 {
//構(gòu)造Scanner對(duì)象的各種方式
public static void main(String[] args) throws IOException{
//1.使用一個(gè)字符串構(gòu)造Canner
Scanner sc1 = new Scanner("Hello world! 12");
while(sc1.hasNext()){
System.out.println(sc1.next());
}
//2.使用File構(gòu)造一個(gè)Scanner對(duì)象
Scanner sc2 = new Scanner(Paths.get("c://a.txt"));
while(sc2.hasNext()){
System.out.println(sc2.nextLine());
}
//3.從標(biāo)準(zhǔn)設(shè)備讀取字符售躁,按回車鍵結(jié)束輸入
Scanner sc3 = new Scanner(System.in);
while(sc3.hasNext()){
System.out.println(sc3.nextInt());? //打印出每一個(gè)整形秀睛,每個(gè)整形用空格分隔
}
}
}