1 chapter 3 stringtokenizer. 2 stringtokenizer class there are bufferedreader methods to read a line...
TRANSCRIPT
1
CHAPTER 3
StringTokenizer
2
StringTokenizer CLASS
There are BufferedReader methods to read a line (i.e. a record) and a character, but not just a single word (i.e. a field).
For example:
William Smith 555-55-5555 76542.56
Lisa Jackson 444-44-4444 77654.78
1. Use method readLine to input data entire line read as a string.
2. Need to break each line into meaningful units of data called tokens.
3. But cannot do this by using BufferedReader.
3
Tokenizing a String
class StringTokenizer» Contained in package java.util» Tokens usually delimited by default “whitespace” characters
(space, tab, newline)» You can specify other delimiters (the character or characters that separate
words)
» Contains methods: – public StringTokenizer(String str, String delimits)
– public int countTokens()– public boolean hasMoreTokens()– public String nextToken(String delimits)– public String nextToken()
4
StringTokenizer class ……
To use the StringTokenizer class, create a StringTokenizer object initialized with the string to break apart.
A token is a sequence of characters separated by white space, comma, tab, newline.
nextToken() method » retrieves the next token from the string tokenizer» Throws NoSuchElementException if no more tokens to return.
countTokens() method » Returns the number of tokens remaining to be returned by nextToken
5
hasMoreTokens() method » Tests if there are more tokens available from this tokenizer’s
string. » When used with nextToken, it returns true as long as nextToken has not yet returned all the tokens in the string, returns false otherwise.
6
Constructor METHODS public StringTokenizer(String theString)
Constructor for a tokenizer using whitespace characters to find tokens in theString.
public StringTokenizer(String theString, String delimeters)Constructor for a tokenizer that will user the characters in the string delimiters as separators to find tokens in theString.
7
Tokenizing a String
Create a StringTokenizer object & store the string in the created object.
StringTokenizer tokenizer =
new StringTokenizer(“Hello there!”);
Use the method nextTokenizer with the object (tokenizer) to retrieve the token from the string.str1 = tokenizer.nextToken();
str1 = tokenizer.nextToken();
8
StringTokenizerClass Exercise
1. BufferedReader keyboard = new
BufferedReader(new InputStreamReader(System.in);
2. StringTokenizer tokenizer;
3. String inputLine, name;
4. int num;
5. double decNum;
6. inputLine = keyboard.readLine();
7. tokenizer = new StringTokenizer(inputLine);
8. name = tokenizer.nextToken();
9. num = Integer.parseInt(tokenizer.nextToken());
10. decNum = Double.parseDouble(tokenizer.nextToken());
Have the following input data:- Mickey 97 158.50
9
Example: StringTokenizer
Display the words separated by any of the following characters: space, new line (\n), period (.) or comma (,).
String inputLine = keyboard.readLine();
StringTokenizer wordFinder =new StringTokenizer(inputLine, " \n.,");
while(wordFinder.hasMoreTokens()){ System.out.println(wordFinder.nextToken());}
Question2bor!tooBee
Entering "Question,2b.or !tooBee." gives this output:
10
… Text File Input (Example)
while (record != null){
System.out.println("Here goes: " + record);
StringTokenizer wordFinder = new StringTokenizer(record,",");
while (wordFinder.hasMoreTokens() ){
System.out.println(wordFinder.nextToken() );}
record = inFile.readLine();
}