Java basics of the scanner (Scanner)

I. Definition:
a simple text can use regular expressions to parse the string and basic types of scanners

Scanner using delimiter input mode into tokens, the default delimiter pattern and matches whitespace. It may then be converted using labeled different method next obtained for the different types of values.

Two constructors:
Here Insert Picture Description
three common methods:
divided into two categories:
existing methods: xxx indicates the type of data, such as byte, int, boolean like.
① Boolean hasNextXxx (): determines whether there is one type of data
②Xxx nextXxx (): Get the next type of data.

import java.io.FileNotFoundException;
import java.util.Scanner;
public class ScannerDemo {
    public static void main(String[] args) throws FileNotFoundException {
        //1.扫描文件中的数据
       // Scanner scanner = new Scanner(new File("file/scanner.txt"),"UTF-8");
        //2.扫描键盘输入的数据
        //Scanner scanner = new Scanner(System.in);
        //3.扫描字符串中的数据
        Scanner scanner = new Scanner("java是一种编程语言");
        while (scanner.hasNextLine()){
            String line = scanner.nextLine();
            System.out.println("ECHO:"+line);
        }
         scanner.close();
    }
}

Four .next () and nextLine () the difference between
next (): read only enter until a space. It can not read the two separated by spaces or symbols words. Further, next () after reading the input cursor on the same line. (read-only space data before Next (), and the cursor is pointing to the Bank)
nextLine (): read the input, spaces and all symbols other than a carriage return (i.e. it reads the line.) between words. After reading the inputs, nextLine () to locate the cursor on the next line.

Published 99 original articles · won praise 2 · Views 2593

Guess you like

Origin blog.csdn.net/weixin_41588751/article/details/105342227