Title: The Scanner Class
1The Scanner Class
- The Scanner actually performs three tasks for us
common in processing input from a character
stream - giving us an object that will read characters
from the stream - tokenizing the input (upon request)
- parsing the input converting characters into a
number - The Scanner class is actually included in the
java.utils package in the next Java release
(1.5), but we will supply you with a pre-release
version (written by Marty Stepp thanks, Marty!) - Java source file, class file and documentation is
on the course web
2Creating an Instance of Scanner
- Using the Scanner constructor to connect a
Scanner to the standard input stream, to a file,
or to a String. The beauty is, once the Scanner
is connected the source of the input (stream,
file, string) becomes irrelevant to the program.
public class Echo public static void main
(String args) Scanner scan new
Scanner(System.in)
public class Echo public static void main
(String args) String mls "Multi
line\nstring\t and more" Scanner scan
new Scanner(mls)
import java.io. public class Echo public
static void main (String args) throws
FileNotFoundException Scanner scan new
Scanner(new File("echo-input.txt"))
3Using the Scanner Just to Read Lines
A line is nothing more than a sequence of 0 or
more characters followed by a newline
character. In line mode, the Scanner extracts
characters from the input up to and including
the next newline character, throws away the
newline, and returns the characters in a String
object.
public class LineEcho public static void
main (String args) Scanner scan new
Scanner() while (scan.hasNextLine())
String nextLine scan.nextLine()
doSomethingToLine(nextLine)
4Using the Scanner to Tokenize
A token (aka a "word") is a contiguous sequence
of non-whitespace characters. When a Scanner
returns the next token, it first strips all
whitespace characters from the beginning of the
input stream. Then it collects characters until
it encounters a whitespace character, leaves that
whitespace character on the input stream, and
returns the characters it collected as a String
public class WordEcho public static void
main (String args) Scanner scan new
Scanner() while (scan.hasNext())
String nextWord (String)scan.next()
doSomethingToLine(nextWord)
5Using the Scanner to do Numeric Parsing
A special case of token-oriented processing is
when the token can be parsed to be an integer
(i.e. it is a string consisting of an
optional negative sign followed by 1 or more
digits). The scanner can first extract this
token, then convert it to an int.
public class IntEcho public static void main
(String args) Scanner scan new
Scanner() while (scan.hasNextInt())
int nextNumber scan.nextInt()
doSomethingToNumber(nextNumber)
6The Scanner and the Class Formerly Known as
InputUtilities
- Last quarter we quickly learned that writing
"bulletproof" code to handle user input is
tedious. Example prompt for and read an
integer between n and m - what if n gt m
- what if the input is not an integer
- what if the input is multiple words
- what if the integer is out of range
- We saw many examples of code that happen commonly
in programs that interact with a user - get a line of text (possibly non-empty)
- get an integer or double (possibly within a
range) - get a date (possibly within a range)
- get a menu selection
- simply print some stuff to the output stream the
user is looking at
7The Old InputUtilities
- User interaction using the console
- uses a Scanner, but that is transparent to the
user - Public interface (subset)
- String getString(String prompt)
- int getInt(String prompt)
- int getYesNo(String prompt)
- int getMenuOption(String prompt, String menu, int
numChoices) - Some changes (as a result of thinking about the
same interface but using a GUI) - these routines are specific to the console
- output should go through the interface too
- the getMenuOption call suffers from us having
done it before we knew about arrays - So the new class is called ConsoleIOUtilities and
it implements an interface IOUtilitiesInterface
8The Interface for All Our User Interaction
We can add things to it, but this will get us
started. We already have the console-based
implementation of it. We can easily build the
Swing-based implementation of it.
public interface IOUtilitiesInterface
String getString(String prompt) int
getMenuOption(String header, String options)
int getInt(String prompt, int low, int high)
int getInt(String prompt) boolean
getYesNo(String prompt) void print(String
text)
9How to Get It?
- Previously we just had copies of Scanner.class
and InputUtilities.class lying around, and copied
them in - This quarter we will be a little more tidy, and
put all of our UI utilities in a package called
ioutilities - More about packages..