Scanner类型

1、Scanner介绍
在这里插入图片描述
2、构造方法
在这里插入图片描述

import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Scanner;

public class Demo3_Scanner {

    public static void main(String[] args) throws FileNotFoundException {
        //构造方法
        //1、Scanner(InputStream) 扫描流对象(键盘扫描)
        InputStream in = System.in;
        Scanner input = new Scanner(in);
        System.out.println(input);//java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q�\E][infinity string=\Q∞\E]

        //2、Scanner(File) 扫描文本
        File file = new File("/Users/jim/Desktop/Work/temp/mail_htmltemplate.txt");
        Scanner input2 = new Scanner(file);
        System.out.println(input2);//java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q�\E][infinity string=\Q∞\E]

        //3、Scanner(String) 扫描 字符串
        Scanner input3 = new Scanner("good good study,day day up");
        System.out.println(input3);//java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q�\E][infinity string=\Q∞\E]

    }
}

3、功能方法
在这里插入图片描述
在这里插入图片描述

import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Scanner;

public class Demo3_Scanner {

    public static void main(String[] args) throws FileNotFoundException {
        //扫描文本
        File file = new File("/Users/jim/Desktop/Work/temp/mail_htmltemplate.txt");
        Scanner input2 = new Scanner(file);

        String str = input2.nextLine();
System.out.println(str);//<!doctype html>

        //扫描字符串
        Scanner input3 = new Scanner("good good study,day day up");
        String s = input3.next();
        System.out.println(s);//good

        //method_hasNextXxx();

        //method_nextXxx();

        // method_构造方法();
    }

    private static void method_hasNextXxx() {
        //hasNextXxx() 判断扫描的值类型是否吻合,返回true/false
        Scanner input = new Scanner(System.in);
        if (input.hasNextInt()) {
            int i = input.nextInt();//java.util.InputMismatchException 输入不吻合错误
            System.out.println(i);
        }

        //close() 释放资源(操作系统资源)
        input.close();
    }

    private static void method_nextXxx() {
        //.nextXxx()
        Scanner input = new Scanner(System.in);
        int i = input.nextInt();
        System.out.println(i);//10

        double d = input.nextDouble();
        System.out.println(d);//2.3

        boolean b = input.nextBoolean();
        System.out.println(b);//true

        //获取字符串首字符
        char c = input.next().charAt(0);
        System.out.println(c);


        //nextLine() 扫描字符串数据,但如果与其他nextXxx一起使用时会导致无法扫描字符串。
        //问题:如何形成?
        //结束next()扫描的方式? 回车键,空格(空字符如tab)
        //结束nextLine()扫描的方式?只能回车键
        //>>>因为nextXxx录入数据时的回车键被nextLine()所获取,导致nextLine直接结束,无法扫描字符串。
        //解决:   1、将所有的nextLine换成next
        //        2、重新获取Scannner对象
        //        3、多写一次nextLine()
        //String str = input.next();
        //System.out.println(str);
        //input = new Scanner(System.in);
        input.nextLine();//上面留下来的获取回车符
        String s = input.nextLine();
        System.out.println(s);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值