Scanner 类

Scanner 类

一个可以使用正则表达式来解析基本类型和字符串的简单文本扫描器。

类声明

public final class Scanner implements Iterator<String>, Closeable {}

这里我们需要关注三个点:

1、final修饰,不能被继承

2、实现了 Iterator 接口(迭代器接口),主要使用其中的 hasNext 方法和 next 方法

3、实现了 Closeable 接口,主要使用其中的 close 方法

// 用来判断是否还有下一个元素录入
public boolean hasNext()

// 用来接收录入的数据
public String next()

// 用来关闭资源(输入流)
public void close()

构造方法

从指定的扫描输入流构造一个 Scanner

public Scanner(InputStream source)

案例代码

public class Demo1 {
	public static void main(String[] args) {
		// java.util.Scanner.Scanner(InputStream source)
        // 使用构造方法,传入一个标准输入流,创建一个Scanner对象
		Scanner sc = new Scanner(System.in);
	}
}

next()

// 此方法用于接收从键盘录入的字符串
public String next()

案例代码

public class Demo1 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        
        // 从键盘录入一个字符串
        String str = sc.next();
        
		System.out.println(str);
	}
}

nextInt()

常用方法
// 此方法用于从键盘获取一个int类型的数据
public int nextInt()

案例代码

public class Demo1 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        
        // 从键盘录入一个字符串
        int num = sc.nextInt();
        
		System.out.println(num);
	}
}
方法的重载:nextInt(int radix)
// 指定进制数从键盘录入一个int类型的数据
public int nextInt(int radix)

int radix参数表示一个进制数,如果从键盘录入的数据超过了指定的进制数就会报错,默认是10

案例代码

public class Demo5 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		/*
		 * 8表示接收8进制内的数,及0~7
		 * 
		 * 从键盘录入的数据中不能出现8及其以上的数字,如果有会报错:
		 * InputMismatchException 输入类型不匹配异常
		 */
		int num = sc.nextInt(8);
		
		System.out.println(num);
	}
}
相关方法:用法类似,要求触类旁通
public String nextLine()
public byte nextByte()
public byte nextByte(int radix)
public short nextShort()
public long nextLong()
public float nextFloat()
public double nextDouble()
public BigInteger nextBigInteger()

hasNextInt()

// 如果此扫描器的输入中有另一个标记,则返回 true。在等待要扫描的输入时,此方法可能阻塞。扫描器将不执行任何输入。
public boolean hasNext()

此方法可用于合法性判断

案例

public class Demo2 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        
		if (sc.hasNextInt()) {
			System.out.println(sc.nextInt());
		} else {
			System.out.println("输入的数据类型有误");
		}
	}
}

其他相关方法

public boolean hasNext()
public boolean hasNextLine()
public boolean hasNextBoolean()
public boolean hasNextByte()
public boolean hasNextShort()
public boolean hasNextLong()
public boolean hasNextFloat()
public boolean hasNextDouble()

扩展:回车换行问题

public class Demo3 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        
		int num = sc.nextInt();
		String str = sc.nextLine();
		
		System.out.println("str:" + str + ",num:" + num);
	}
}

当从键盘录入第一个数字后,敲下的回车键实际是两个字符:\r\n,所以键盘实际是录入了这两个字符并且结束,显示的效果和没有录入字符串相同

补充:

\r:回车,回到行首

\n:换行

电脑上的回车键(Enter键)实际上是回车式换行,会先执行回车,后执行换行

扩展:多行录入

可以通过hasNext的重载方法来实现多行录入

// 如果从键盘录入指定的pattern标记,则返回true,否则返回false
public boolean hasNext(String pattern)

案例

public class Demo4 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		StringBuffer sb = new StringBuffer();

        // 将#作为结束的标记,当遇到#时结束循环
		while (!sc.hasNext("#")) {
			sb.append(sc.next());
		}
        
		System.out.println(sb);
	}
}

总结

Scanner 通过标准输入流来从键盘输入,后期不会用到,目前常用的方法为nextXxx()和hasNextXxx()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值