A Whitespace character is a character that appears as white space when printed or displayed (on a white background). The space character, the tab character, and the newline character are all examples of whitespace characters. A token is a string of characters that contains no whitespace characters. Since whitespace characters cannot be part of a token, whitespace characters serve to separate one token from the next. We say that the tokens are whitespace delimited.
A scanner extracts tokens (one at a time) from a stream of characters and throws away the intervening whitespace characters. Given a token, the scanner tries to convert the text in that token into the type of value that the program has indicated it is looking for. For example, if the application is looking for a double value then the scanner will convert the token into the corresponding double value. If the token text is not a valid representation of the specified data type then an error occurs.
There is one exception to the definition of a token given above. The application program can specify that the next token be the next line of text including any whitespace within that line.
Scanner(File source) Scanner(InputStream source) Scanner(String source)
Notice that each constructor requires you to specify the source of the text that the Scanner object will be scanning. The source can be a text file, an input stream (such as System.in) or a string.
boolean hasNext() String next()
The "hasNext" method returns true if there is another token in the stream and false otherwise. The "next" method returns the next token as a string value.
boolean hasNextDouble() double nextDouble() boolean hasNextInt() int nextInt()
The "hasNextDouble" and "hasNextInt" methods return true if there is a next token and that token is a valid representation of the specified data type. Otherwise, a value of false is returned. The "nextDouble" method scans the next token, converts it to a double value and returns that value. The "nextInt" method scans the next token, converts it to an integer value and returns that value.
boolean hasNextLine() String nextLine()
The "hasNextLine" method returns true if there is another line of text in the input stream and false otherwise. The "nextLine" method returns the next line (everything from the current position in the stream to the end of the line including any whitespace) as a string value.
See also: System