27.扫描器类

java.util.Scanner类:扫描器类,表示输入操作.

存在的方法:xxx表示数据类型,入byte,int,boolean等等

boolean hasNextXxx():判断是否有下一种数据类型的数

Xxx: nextXxx()):获取下一个该类型的数据
示例代码
public class ScannerDemo {
    public static void main(String[] args) throws Exception {
        
        //扫描文件中的数据
        //test1();
        
        //扫描键盘中输入的数据
        //test2();
        
        //扫描字符串中的数据
        test3();
    }
    
    private static void test1() throws Exception {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(new File("file/123.txt"));
        while(sc.hasNextLine()){
            String str = sc.nextLine();
            System.out.println(str);
        }
        sc.close();
    }

    private static void test2() throws Exception {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextLine()){
            String str = sc.nextLine();
            System.out.println("ECHO:"+str);
        }
        sc.close();
    }

    private static void test3() {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner("为何你如此之帅");
        while(sc.hasNextLine()){
            String str = sc.nextLine();
            System.out.println(str);
        }
        sc.close();
    }
    //
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容