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
分析
-
从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 variousnext
methods. -
自己理解,首先“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 Readable
interface. 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帮助文档