Download presentation
Presentation is loading. Please wait.
Published byMiranda Ross Modified over 9 years ago
1
1 CHAPTER 3 StringTokenizer
2
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-555576542.56 Lisa Jackson 444-44-444477654.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
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
4 StringTokenizer class …… To use the StringTokenizer class, create a StringTokenizer object initialized with the string to break apart. l 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
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
6 Constructor METHODS l public StringTokenizer(String theString) Constructor for a tokenizer using whitespace characters to find tokens in theString. l 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
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();
8
8 StringTokenizer Class 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
9 Example: StringTokenizer l 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()); } Question 2b or !tooBee Entering " Question,2b.or !tooBee. " gives this output:
10
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(); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.