Token and Line Based Proccessing

advertisement
Files Review
• Files are data stored on a disk (or even in
memory).
• Really all files are binary, but some files
translate into human readable text without
using “special” applications to read them
(.txt files).
• The File object is an encapsulation of
state and behavior for Files.
Files Review (con’t)
• There are a File object is an
encapsulation of state and behavior for
Files.
• Once you create a File object with the
pathname of the file you wish to open, you
can attach a Scanner object to that file for
reading it.
Token Based Processing
• Introduced in 3.4 – when we started with
interactive programs.
• Reminder: A Token is a single element of input
(one word, one number, one line), separated by
whitespace (spaces, tabs, new lines).
• Scanner methods we’ve potentially used:
next(), nextDouble(), nextInt(),
nextLine(), hasNext(),
hasNextDouble(), hasNextInt()
Cursors and Scanner
• A cursor is a moving placement or pointer that
indicates a position. English-speakers have
used the term with this meaning since the 16th
century, for a wide variety of movable or mobile
position-markers. (From Wikipedia:
http://en.wikipedia.org/wiki/Cursor)
• A Scanner views all input (from the console or
from a file) as a stream of characters. The
Scanner keeps track of what character it’s
viewing with its input cursor
Examples
• The input cursor starts at the beginning of the stream of
text. Remember that after each line there is a \n
character
The
rain in Spain falls 8 times.\n
^
2.0 4\n 6 more text
• After Java executes, the cursor points to the white space
immediately following the token
String s = Scanner.next()
The
rain in Spain falls 8 times.\n
^
2.0 4\n 6 more text
s == “The”
More Examples
• After Java executes
String s1 = Scanner.nextLine()
The
rain in Spain falls 8 times.\n
2.0 4\n 6 more text
^
s == “rain in Spain falls 8 times.”
• After Java executes
double d = Scanner.nextDouble()
The
rain in Spain falls 8 times.\n
2.0 4\n 6 more text
^
d == 2.0
And Even more examples
• After Java executes
int i = Scanner.nextInt()
The rain in Spain falls 8 times.\n
2.0 4\n 6 more text
^
i == 4
• After Java executes
d = Scanner.nextDouble()
The rain in Spain falls 8 times.\n
2.0 4\n 6 more text
^
i == 6.0
And yet more…
• After Java executes
Scanner.nextDouble()
The rain in Spain falls 8 times.\n
2.0 4\n 6 more text
^
NoSuchElementException
Processing
• Processing input can be on an individual token
level (Token-based processing) OR on a line-byline basis (Line-based processing)
• nextLine returns a line as a String.
– The Scanner moves the cursor until it sees a new
line character ‘\n’ and returns the whole text.
– The \n character is consumed but not returned.
• Example
2.0 4\n 6 more text\n
^
String sLine = console.nextLine();
sLine.equals(“2.0 4”) == true
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?…
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
“”
2.0\n
Code says:
Java at this point has only
consumed the new line
character!
console.next() at this point can
now accept new input from the user!
^
hasNext*
• Scanner methods hasNext(),
hasNextLine(), hasNextDouble(),
hasNextInt() also work on a File.
• These are good for making sure the next
bit of input is exactly what you think it
*should* be.
• Good for a robust interaction
Program #6 – Options 1, 2
1. Book Project #2, page 373-4. (diff) with the
following required additions: The user should
be able to continue to compare files for as long
as they want AND allow the user to write the
differences to a file separate.
2. Book Project #4, page 374 (Popularity of Baby
names) with the following required additions:
write everything that goes to the console also
to a file. I will give you text files that have the
baby names. The drawing panel part is not
required, but highly recommended.
Program #6 – Option 3
3.
MadLibs ™.
A MadLibs file will be a text file that has certain
keywords in square brackets []. For example, the
Beatle’s Eleanor Rigby.
Ah, look at all the [adjective] [plural noun]!
Ah, look at all the [adjective] [plural noun]!
[Girl's name] [Last name] picks up the [noun] in a
[place] where a [event] has been.
[verb] in a [noun].
[present tense verb] at the [noun], wearing the
[noun] that she keeps in a [noun] by the [noun]. Who
is it for?
All the [adjective] [plural noun], where do they all
come from?
All the [adjective] [plural noun], where do they all
[verb]?
Program #6 – Option 3 (con’t)
3. MadLibs ™. (continued)
For either version: Prompt the user for the
mad lib file and an output file name. As
the file is read in, fill in the “blank” words
with the type denoted by the word in
brackets. Output the completed version to
a file. Open the output file and print it to
the console to show the user what the
result is.
Program #6 – Option 3 (con’t)
3.
There are two ways to get the words to use for the “blanks”
1.
2.
Prompt the user for each of the words as they are encountered on the
command line
Read in a third file, one that contains pairs that are in the form
part-of-speech fill-in-word
such as
adjective happy
adjective nerpy
adjective tired
car-brand mazda
person-name Hilda
Your file must have enough of each type to cover a good sized mad
lib. You can use arrays and random numbers to pick between the
types of mad lib keywords. As a class we may want to pre-define
valid allowable types.
Program #6 Milestones
• Milestone 1 - Due Wednesday 11/21 by 5pm in email
– Your Requirements
• Which Option you’re picking, 1, 2, 3a, or 3b
• Any assumptions you are making
• Possibly any drawings you feel like adding to the requirements
– Detailed Design
• pseudo code or diagram that shows functional breakdown
– Design Review will be IN CLASS on Wednesday 11/28
• IMPORTANT: DO NOT touch your design over break. I will take
points off if I find out you’ve started to code before 11/28.
• Milestone 2 – Due Wednesday 12/5 by 5pm in email
– Your .java file(s)
• Each file/class should contain readable code with comments
• Your .java file should have the appropriate name and header comment
– Sample output from your program (in a .txt file – the file you generate)
– Tell me what (if anything) changed from your requirements/design
phase (either in email or a .txt file)
Program 6 Grading
Grading will be based on:
• Design must be well thought out and understandable
• Code must be commented. NOTE: you must have your
header at the top and JavaDoc type comments for each
method
• I must be able to compile and execute the code you give
me
• The output you give me must match the requirements
you stated in Milestone 1 or you must explain why it was
different.
• Constructs that have been taught are used such as:
• Everything I’ve graded you on before (see
program #5)
• File processing (input/output)
Reminders….
• Name of your classes should be <yourname><classname>.
Example
public class brickerRocketShip {
• At the top of the file I want the following
// Your Name
// Homework number
// Brief description of what assignment you’re doing
// Classname (i.e. the name of the class in the file)
// Version information
// Date
• Each method should have comments above it that would
generate JavaDoc
Download