Lecture Thirteen

Download Report

Transcript Lecture Thirteen

String line = console.readLine()
String line = console.readLine()
StringTokenizer st = new StringTokenizer(line)
String line = console.readLine()
StringTokenizer st = new StringTokenizer(line)
st.hasMoreTokens()
String line = console.readLine()
StringTokenizer st = new StringTokenizer(line)
st.hasMoreTokens()
true
String line = console.readLine()
StringTokenizer st = new StringTokenizer(line)
st.hasMoreTokens()
true
String token = st.nextToken()
String line = console.readLine()
StringTokenizer st = new StringTokenizer(line)
st.hasMoreTokens()
true
String token = st.nextToken()
process the token somehow
String line = console.readLine()
StringTokenizer st = new StringTokenizer(line)
st.hasMoreTokens()
false
true
String token = st.nextToken()
process the token somehow
String line = console.readLine()
StringTokenizer st = new StringTokenizer(line)
st.hasMoreTokens()
false
true
String token = st.nextToken()
process the token somehow
String line = console.readLine()
StringTokenizer st = new StringTokenizer(line)
st.hasMoreTokens()
false
true
String token = st.nextToken()
process the token somehow
tokenizer is empty (but line is untouched)
String line = console.readLine()
StringTokenizer st = new StringTokenizer(line)
st.hasMoreTokens()
false
true
String token = st.nextToken()
process the token somehow
thus st = new StringTokenizer(line) allows us to reprocess the string (if we need to)
String line = console.readLine()
StringTokenizer st = new StringTokenizer(line)
st.hasMoreTokens()
false
true
String token = st.nextToken()
process the token somehow
thus st = new StringTokenizer(line) allows us to reprocess the string (if we need to)
String line = console.readLine()
StringTokenizer st = new StringTokenizer(line)
st.hasMoreTokens()
false
// sum is defined and initialized to zero outside
try {
true
int number = Integer.parseInt(token);
sum = sum + number;
String token = st.nextToken()
} catch (Exception e) {
process the token somehow
System.out.println(token + “ not a number!”)
}
thus st = new StringTokenizer(line) allows us to reprocess the string (if we need to)