一、明来路:
public final class Scanner implements Iterator<String>, Closeable {
// Internal buffer used to hold input
private CharBuffer buf;
// Size of internal character buffer
private static final int BUFFER_SIZE = 1024; // change to 1024;
// The index into the buffer currently held by the Scanner
private int position;
// Internal matcher used for finding delimiters
private Matcher matcher;
// Pattern used to delimit tokens
private Pattern delimPattern;
// Pattern found in last hasNext operation
private Pattern hasNextPattern;
// Position after last hasNext operation
private int hasNextPosition;
// Result after last hasNext operation
private String hasNextResult;
// The input source
private Readable source;
// Boolean is true if source is done
private boolean sourceClosed = false;
// Boolean indicating more input is required
private boolean needInput = false;
// Boolean indicating if a delim has been skipped this operation
private boolean skipped = false;
// A store of a position that the scanner may fall back to
private int savedScannerPosition = -1;
// A cache of the last primitive type scanned
private Object typeCache = null;
// Boolean indicating if a match result is available
private boolean matchValid = false;
// Boolean indicating if this scanner has been closed
private boolean closed = false;
// The current radix used by this scanner
private int radix = 10;
// The default radix for this scanner
private int defaultRadix = 10;
// The locale used by this scanner
private Locale locale = null;
// A cache of the last few recently used Patterns
private PatternLRUCache patternCache = new PatternLRUCache(7);
// A holder of the last IOException encountered
private IOException lastException;
// Number of times this scanner's state has been modified.
// Generally incremented on most public APIs and checked
// within spliterator implementations.
int modCount;
// A pattern for java whitespace
private static Pattern WHITESPACE_PATTERN = Pattern.compile(
"\\p{javaWhitespace}+");
// A pattern for any token
private static Pattern FIND_ANY_PATTERN = Pattern.compile("(?s).*");
// A pattern for non-ASCII digits
private static Pattern NON_ASCII_DIGIT = Pattern.compile(
"[\\p{javaDigit}&&[^0-9]]");
// Fields and methods to support scanning primitive types
/**
* Locale dependent values used to scan numbers
*/
private String groupSeparator = "\\,";
private String decimalSeparator = "\\.";
private String nanString = "NaN";
private String infinityString = "Infinity";
private String positivePrefix = "";
private String negativePrefix = "\\-";
private String positiveSuffix = "";
private String negativeSuffix = "";
二、知用处
import java.util.Scanner;
public class ATestForScanner {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
if (scanner.hasNextInt()) {
int i = scanner.nextInt();
System.out.println(i);
}
//hasNextInt为true时
//才会执行nextInt的读入
//两个大多数配套使用
//此外使用 next() 和 next八种基本数据冲突,会吞输入,因为具体的nextInt是一块一块读取的
//nextInt 输入 每次输入一个数字,用回车表示输出结束
System.out.println(scanner.next());
scanner.close();
}
}
此外用完记得关闭,这是个好习惯