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 User types in 2.0, then hits enter but what comes into java is: 2.0\n ^ 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! 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. See drawthingfromfile2.java
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... input.nextline();
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, else else p.x, p.y); pold = new Point(p); // 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 object associated with the variable foo
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 stay on top - even if you use DrawingPanel.toFront()