Text-based scanner 10. Scanner java.util
- A regular expression can be used to parse simple text strings and basic types of scanners.
Construction method of receiving System.in
, and File
InputStream
- public Scanner (File source): Constructs a new Scanner, which produces values scanned from the specified file.
- public Scanner (InputStream source): Constructs a new Scanner, produces values specified from the input stream is scanned.
- public Scanner (String source): Constructs a new Scanner, produces values scanned from the specified string.
Common method , , , hasNext()
hasNextXx()
next()
nextXx()
- public boolean hasNext (): If the input of this scanner has another flag, it returns true.
- public boolean hasNextLine (): If there is input of this scanner is another line, it returns true.
- public boolean hasNextInt (): a default radix int Returns true if the method, a tag scanner's input this information by using the nextInt () can be interpreted. The scanner does not perform any input.
- public boolean hasNextInt (int radix): if () method, this scanner's input information by a tag can be interpreted as nextInt using an int value specified radix, it returns true. The scanner does not perform any input.
hasNextBigDecimal()
,hasNextBigInteger()
,hasNextBoolean()
,hasNextByte()
,hasNextDouble()
,hasNextFloat()
,hasNextLong()
,hasNextShort()
- public String next (): Finds and returns the next complete token from this scanner.
- public String nextLine (): This scanner past the current line, and returns the input skipped.
- public int nextInt (): the input of the next scanning information marker as an int.
- public int nextInt (int radix): the input of the next scanning information into a tag int, radix used to interpret the token int cardinality.
nextBigDecimal()
,nextBigInteger()
,,,,,,NextBoolean()
NextByte()
NextDouble()
NextFloat()
NextLong()
NextShort()
- public void close (): Closes this scanner.
Knowledge extension
1, the receiving console input
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
//String name = sc.nextLine();
int age = sc.nextInt();
//float salary = sc.nextFloat();
if (age > 100) {
age = 100;
}
2, next () and nextLine () difference
next():
1、一定要读取到有效字符后才可以结束输入。
2、对输入有效字符之前遇到的空白,next() 方法会自动将其去掉。
3、只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符。
next() 不能得到带有空格的字符串。
nextLine():
1、以Enter为结束符,也就是说 nextLine()方法返回的是输入回车之前的所有字符。
2、可以获得空白。
<https://blog.csdn.net/ZytheMoon/article/details/79293516>