Token vs Line Based Processing
• Mixing token based processing and line
based processing can be tricky…
particularly on the console
• Problems I saw in program 5:
console.next() <- the first call returns a string …
console.next() <- the second call returns an empty string.
Why Didn’t that work?…
Code says: console.nextDouble() returns “2.0” console.next() returns “”Java at this point has only consumed the new line character!
console.next() at this point can now accept new input from the user!
User types in 2.0, then hits enter but what comes into java is:
2.0\n
2.0\n
^ ^
Tokenizing Strings
• A
Scanner
can also work just on
String
objects
Scanner <name> = new
Scanner(<String>);
• Example:
String text = “This is a sentence";
Scanner scan = new Scanner(text);
// There are 4 tokens …
For Files
• Read in each line of the file, then use the
Scanner
to break apart each line.
Scanner input = new Scanner(new File(sFileName)); while ( input.hasNextLine() )
{
String line = input.nextLine();
Scanner lineScan = new Scanner(line); // process the line
FYI: Passing Scanner
(drawThingFromFile.java)
Scanner input =
new Scanner( fObjectFile ); …
lineColor = readColor(input); …
public static Color readColor( Scanner input ) {
… }
Complex Input files
• Requirements: Read in a file of the following format
width height
bkcolor red green blue
color red green blue
point x y
• Anything coded in green are integer values
• The width/height must be the first two integers in the file • After that the bkcolor, color , and point values can be in
any order
• The keywords must be followed by their values integer values
• Bring up a drawing panel, draw lines between the points, with the background color and line color last specified. • Be robust in handling problems in the file.
Two Designs
• Design 1: Use a combination of
Scanner.next(), Scanner.nextInt(), etc.
– See drawThingFromFile.java
• Design 2: Use Scanner.nextLine() to get
each line of text, then tokenize each line of
text.
Design 1
Getting Width and Height
Scanner input = new Scanner( fObjectFile ); int width = 400; int height = 400; if ( input.hasNextInt() ) { width = input.nextInt(); } if ( input.hasNextInt() ) { height = input.nextInt(); }
// just chewing a new line to // get to the next line...
Design 2
Getting Width and Height
Scanner input = new Scanner( fObjectFile ); String sLine = "";
int width = 400; int height = 400;
if ( input.hasNextLine() ) { sLine = input.nextLine();
Scanner lineScanner = new Scanner( sLine ); if ( lineScanner.hasNextInt() ) { width = lineScanner.nextInt(); } if ( input.hasNextInt() ) { height = input.nextInt(); } }
Design 1 – Reading Attributes
while ( input.hasNext() ) {
// first get and compare the // keyword, then get the
// information based on the keyword sLabel = input.next(); if ( sLabel != null ) { if ( sLabel.equalsIgnoreCase("color") ) { lineColor = readColor(input); if ( lineColor != null ) g.setColor(lineColor); } else if ( sLabel.equalsIgnoreCase("bkcolor") ) { bkColor = readColor(input); if ( bkColor != null ) panel.setBackground(bkColor); } else if ( sLabel.equalsIgnoreCase("point") ) { if ( pOld != null ) pOld.setLocation(p); if ( readPoint(input, p) ) { if ( pOld != null ) g.drawLine(pOld.x, pOld.y, p.x, p.y); else
pOld = new Point(p); }
}
else {
// error, just dump the line. input.nextLine(); } } else { input.nextLine(); } }
Design 2 – Reading attributes
while ( input.hasNextLine() ) {
sLine = input.nextLine();
if ( sLine == null || sLine.trim().equals("") ) {
continue;
}
Scanner lineScanner = new Scanner( sLine ); // first get and compare the keyword, then // get the information based on the keyword sLabel = lineScanner.next(); if ( sLabel.equalsIgnoreCase("color") ) { lineColor = readColor(lineScanner); if ( lineColor != null ) g.setColor(lineColor); } else if ( sLabel.equalsIgnoreCase("bkcolor") ) { bkColor = readColor(lineScanner); if ( bkColor != null ) panel.setBackground(bkColor); } else if ( sLabel.equalsIgnoreCase("point") ) { if ( pOld != null ) pOld.setLocation(p); if ( readPoint(lineScanner, p) ) { if ( pOld != null )
g.drawLine(pOld.x, pOld.y, p.x, p.y); else
pOld = new Point(p); }
New Things
• Two new Java keywords were in Design 2:
null
and
continue
• null
means that there is no object.
Example:
–
String foo;
– If
foo ==
null
– that means there is no
The
null
Keyword
• null
means that there is no object. Example:
– String foo;
– If foo == null – that means there is no object associated with the variable foo
if ( sLine == null || sLine.trim().equals("") ) {
continue; }
The
continue
Keyword
• Continue
is used in loops.
– It means “skip the rest of the controlled statements in the loop and go back to the “test statement” and start again. Example:
while ( input.hasNextLine() ) {
if ( sLine == null || sLine.trim().equals("") ) {
continue; }
<skipped statements> }
Input/Output and Graphics
• Problems that have input/output and graphical
output, break the problem into pieces:
Text input/output, file I/O first
– Welcome message, get the file name if necessary – Open the file, and get the data.
– Process the data – Produce text output
– Do the graphical output.
• Annoying but true: You can do I/O interactively
with the console, but the DrawingPanel won’t