Title: Deadlines
102-17
- Deadlines
- Assignment 5 due Thursday 2/17, 415PM
- deadline for 5 extended to Tuesday 2/22
- Assignment 6 Assigned 2/18, due 3/1
- Second midterm exam Thursday 2/24
- Last time
- major components of input loops
- input verification versus user input stream
versus file input stream - what operation on the stream
- do something to every record
- accumulate information about all the records
- sum/count, min/max, any/every
- combinations of the above
- This time
- a little review
- loop over sequences
- nested loops
- collections, positional access, and the Iterator
interface
2Loop Mechanics
- Establish the input stream
- in the case of user input, establish how you want
to stop and whether you will allow no input at
all
InputUtilities iu new InputUtilities() //
maybe some initialization here while (true)
String nextInput iu.getLine("Next input ")
if (stopIndicator(nextInput)) break
doSomethingToInput(nextInput) // maybe some
output here
InputUtilities iu new InputUtilities() //
maybe some initialization here boolean moreInput
true while (moreInput) String nextInput
iu.getLine("Next input ") doSomethingToInput(n
extInput) moreInput iu.getYestNo("More
input? ") // maybe some output here
3Don't Be Fooled By Small Variations in Form
InputUtilities iu new InputUtilities() //
maybe some initialization here while (true)
String nextInput iu.getLine("Next input ")
doSomethingToInput(nextInput) if
(!iu.getYesNo("More input? ")) break //
maybe some output here
InputUtilities iu new InputUtilities() //
maybe some initialization here boolean moreInput
true while (moreInput) String nextInput
iu.getLine("Next input ") doSomethingToInput(n
extInput) moreInput iu.getYesNo("More
input?") // maybe some output here
non-local exit versus sentinel is a matter of
style prompting for more input versus input
termination value is an important design decision
4Another Small Variant While versus Until
int sum 0 do sum iu.getInt("Next
input ") while (iu.getYesNo("More input? "))
int sum 0 while (iu.getYesNo("More input? "))
sum iu.getInt("Next input ")
5Loop Mechanics for File Input
Scanner scf new Scanner(new File(FILE_NAME)) /
/ maybe some initialization here while
(scf.hasNext()) String nextToken
(String)scf. next() doSomethingToToken(nextToke
n) // always need to handle possibility of no
input
Scanner scf new Scanner(new File(FILE_NAME)) /
/ maybe some initialization here while
(scf.hasNextLine()) String nextLine scf.
nextLine() doSomethingToLine(nextLine) //
always need to handle possibility of no input
Whether or not to do line-oriented or
token-oriented processing is a major design
decision. Loop control is essentially the same
in either case.
6Next Choose Your Processing Idiom
int sum 0 while (scf.hasNext()) int
nextNumber scf.nextInt() sum
nextNumber // postcondition variable sum
// holds result
int max Integer.MIN_VALUE if (!scf.hasNext())
// maximum not defined reportBadMaximum()
else while (scf.hasNext()) int
nextNumber scf.nextInt() max
Math.max(nextNumber, max) // postcondition
variable max holds // result unless no input
boolean every true while (scf.hasNext())
Object nextThing scf.next() if
(!propertyHoldsFor(nextThing)) every
false break // postcondition
variable every // holds result
7Overall Lesson About Input Stream Processing
- Role of the loop structure is (only) to control
when to quit - Type of input (file or console, token or line)
determines the input idiom - Type of processing task (calculate and output,
accumulate) determines the processing idiom - When designing new code, think of these decisions
in isolation, not as part of one big mess!