Basics
- Java uses Scanner to provide a text view of an input data stream.
Important constructors for creating Scanners
- Suppose s is a String, f is a File, and u is a URL.
- Scanner stream = new Scanner( System.in );
- Makes stream a variable giving a Scanner view of standard input.
- Makes stream a variable giving a Scanner view of standard input.
- Scanner stream = new Scanner( f );
- Makes stream a variable giving a Scanner view of the file represented by f.
- Makes stream a variable giving a Scanner view of the file represented by f.
- Scanner stream = new Scanner( u.openStream() );
- Makes stream a variable giving a Scanner view of URL u.
- Makes stream a variable giving a Scanner view of URL u.
- Scanner stream = new Scanner( s );
- Makes stream a variable giving a Scanner view of the character string represented by s.
- Makes stream a variable giving a Scanner view of the character string represented by s.
Important methods for getting input
- Suppose stream is a Scanner.
- stream.next() returns the next input value as a String.
- stream.nextInt() returns the next input value as an int.
- stream.nextDouble() returns the next input value as a double.
- stream.nextBoolean() returns the next input value as a boolean.
- stream.nextLine() returns the remainder of the current input line as a String.
Important methods for determining whether there is more input
- Suppose stream is a Scanner.
- stream.hasNext() returns a boolean value indicating whether there is any nonblank input left in the input stream.
- stream.hasNextLine() returns a boolean value indicating whether there is another line of input (i.e., is there an unprocessed new line character).
- stream.hasNextInt() returns a boolean value indicating whether the next input is integer. If there is no more input, the method returns false.
- stream.hasNextDouble() returns a boolean value indicating whether the next input is decimal. If there is no more input, the method returns false.
- stream.hasNextBoolean() returns a boolean value indicating whether the next input is boolean. If there is no more input, the method returns false.