Scanner类

Scanner类

今天我来简单的介绍一下在 java.util包下的Scanner类,Scanner顾名思义,扫描的意思。

public final class Scanner
extends Object
implements Iterator<String>, Closeable
//它被“public final”修饰,表是公有的不能被修改.
// 实现了 Iterator 接口

1.简单的输入一个数

For example,this code allows a user to read a number from System.in:

package fy.com;
import java.util.Scanner;

public class ScannerTest {
    public static void main(String[] args){
              Scanner sc = new Scanner(System.in);
              int i = sc.nextInt();
              System.out.println(i);

    }
}
// 输入 :100
// 输出 :100

分析

  1. 从API上摘抄的原话:A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods.

  2. 自己理解,首先“Scanner sc = new Scanner(System.in);”创建了一个对象 “sc” 其中传入了System.in。(“System.in”我不知道传入的是什么具体属性,望大佬在评论区解答)

    之后定义了一个变量 i将sc.nextInt();赋值给它。

    2.输入文件

 Scanner sc = new Scanner(new File("文件名"));
      while (sc.hasNextLong()) {
          long aLong = sc.nextLong();
      }
// 

3.处理字符

package fy.com;

import java.util.Scanner;

public class ScannerTest01 {
    public static void main(String [] args){
          String sc ="1 cat 2 cat red cat blue cat";
          Scanner s = new Scanner(sc).useDelimiter("\\s*cat\\s*");
          System.out.println(s.nextInt());
          System.out.println(s.nextInt());
          System.out.println(s.next());
          System.out.println(s.next());
          s.close();
          

    }
}
//输出结果:
//1
//2
//red
//blue

第二种方法实现

package fy.com;

import java.util.Scanner;
import java.util.regex.MatchResult;

public class ScannerTest02 {
    public static void main(String[] args) {
        String input = "1 cat 2 cat red cat blue cat";
        Scanner s = new Scanner(input);
        s.findInLine("(\\d+) cat (\\d+) cat (\\w+) cat (\\w+)");
        MatchResult result = s.match();
        for (int i = 1; i<= ((MatchResult) result).groupCount(); i++)
            System.out.println(result.group(i));
        s.close();
    }
}

API中的解释:

The default whitespace delimiter used by a scanner is as recognized by Character.isWhitespace. The reset() method will reset the value of the scanner’s delimiter to the default whitespace delimiter regardless of whether it was previously changed.

A scanning operation may block waiting for input.

The next() and hasNext() methods and their primitive-type companion methods (such as nextInt() and hasNextInt()) first skip any input that matches the delimiter pattern, and then attempt to return the next token. Both hasNext and next methods may block waiting for further input. Whether a hasNext method blocks has no connection to whether or not its associated next method will block.

The findInLine(java.lang.String), findWithinHorizon(java.lang.String, int), and skip(java.util.regex.Pattern) methods operate independently of the delimiter pattern. These methods will attempt to match the specified pattern with no regard to delimiters in the input and thus can be used in special circumstances where delimiters are not relevant. These methods may block waiting for more input.

When a scanner throws an InputMismatchException, the scanner will not pass the token that caused the exception, so that it may be retrieved or skipped via some other method.

Depending upon the type of delimiting pattern, empty tokens may be returned. For example, the pattern "\\s+" will return no empty tokens since it matches multiple instances of the delimiter. The delimiting pattern "\\s" could return empty tokens since it only passes one space at a time.

A scanner can read text from any object which implements the Readableinterface. If an invocation of the underlying readable’s Readable.read(java.nio.CharBuffer) method throws an IOException then the scanner assumes that the end of the input has been reached. The most recent IOException thrown by the underlying readable can be retrieved via the ioException() method.

When a Scanner is closed, it will close its input source if the source implements the Closeable interface.

A Scanner is not safe for multithreaded use without external synchronization.

Unless otherwise mentioned, passing a null parameter into any method of a Scanner will cause a NullPointerException to be thrown.

A scanner will default to interpreting numbers as decimal unless a different radix has been set by using the useRadix(int) method. The reset() method will reset the value of the scanner’s radix to 10 regardless of whether it was previously changed.

总结:

其实在Scanner类中有很多方法,我并不能一一列举。建议大家多去看API帮助文档

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

“逢雨”

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值