• No results found

Token vs Line Based Processing

N/A
N/A
Protected

Academic year: 2021

Share "Token vs Line Based Processing"

Copied!
15
0
0

Loading.... (view fulltext now)

Full text

(1)

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.

(2)

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

^ ^

(3)

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 …

(4)

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

(5)

FYI: Passing Scanner

(drawThingFromFile.java)

Scanner input =

new Scanner( fObjectFile ); …

lineColor = readColor(input); …

public static Color readColor( Scanner input ) {

… }

(6)

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.

(7)

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.

(8)

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...

(9)

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(); } }

(10)

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(); } }

(11)

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); }

(12)

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

(13)

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; }

(14)

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> }

(15)

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

stay on top - even if you use

References

Related documents

The image input is usually a matrix (for grayscale images - dimensions are width, height) or an array (for color and alpha images - dimensions are width, height, planes) of reals.

Here each character is bounded by a fixed height (height of the box) and a fixed width (width of.. Select the starting point of a character and from that height and width a

PC2 explained 17.1% of total morphological variability, with the variables stem diameter, plant height, plant width, leaf color, leaf length, leaf width, fruit length, fruit

Draws a solid rectangle with the specified width and height in the current background color. public void drawRoundRect( int x, int y, int width, int height, int

In order the border selectors’ shorthand properties are width, color, and style. In order the font selector’s shorthand properties are font-style, font-weight, font- size,

In this work, a structural member of rectangular section is analyzed for different (b/h) (Section height / width) values using second order finite difference

Height: 40 pixels File size: up to 50KB File Types: JPG, GIF, PNG Project medium banner http://images.domain.com.au/img/Agencys/devproject/banner_436.png Yes Width: 720

 Import Point Cloud from File - creates a point cloud dataset from an ESRI