1 Overview
1.1 JDK provided Scanner class for obtaining the keyboard input ;
1.2 Scanner class is based on a regular expression is a text scanner , a file may be parsed from the input stream, the basic character string type value, a string value;
1.3 Scanner class provides a plurality of different constructors , you can accept the file input stream, string as the data source, for parsing the data from the file input stream, string;
public final class Scanner implements Iterator<String>, Closeable { =====构造器 private Scanner(Readable source, Pattern pattern) { assert source != null : "source should not be null"; assert pattern != null : "pattern should not be null"; this.source = source; delimPattern = pattern; buf = CharBuffer.allocate(BUFFER_SIZE); buf.limit(0); matcher = delimPattern.matcher(buf); matcher.useTransparentBounds(true); matcher.useAnchoringBounds(false); useLocale(Locale.getDefault(Locale.Category.FORMAT)); } public Scanner(File source) throws FileNotFoundException { this((ReadableByteChannel)(new FileInputStream(source).getChannel())); } public Scanner(String source) { this(new StringReader(source), WHITESPACE_PATTERN); } public Scanner(InputStream source) { this(new InputStreamReader(source), WHITESPACE_PATTERN); } }
1.4 Scanner class method provides:
hasNextXX () : Are there next;
nextXX () : Get the next one;
import java.util.Scanner; public class TestScanner { public static void main(String[] args){ boolean flag=true; while (flag){ //System.in标准输入,即键盘输入 Scanner scanner=new Scanner(System.in); String inputContent=scanner.next(); if (inputContent.equals("exit")){ flag=false; } System.out.println(inputContent); } } }
hasNextLine () : Is there a next line;
nextLine () : Gets the next row;