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.

Slides:



Advertisements
Similar presentations
1 StringBuffer & StringTokenizer Classes Chapter 5 - Other String Classes.
Advertisements

Lecture 15: I/O and Parsing
This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
Introduction to Objects and Input/Output
©2004 Brooks/Cole Chapter 3 Interactive Input. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Standard Input and Output System.in Is Used to.
Chapter 9 Streams and File I/O Overview of Streams and File I/O
Chapter 3, More on Classes & Methods Clark Savage Turner, J.D., Ph.D. Copyright 2003 CSTurner, from notes and text.
Chapter 7: The String class We’ll go through some of this quickly!
Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer.
HST 952 Computing for Biomedical Scientists Lecture 8.
CS102--Object Oriented Programming Lecture 5: – Arrays – Sorting: Selection Sort Copyright © 2008 Xiaoyan Li.
1 String Buffer & String Tokenizer Overview l Overview of String Buffer class and Methods l Overview of String Tokenizer class and methods l Preview: Notions.
COMP 14 Introduction to Programming Miguel A. Otaduy May 17, 2004.
1 Text File I/O  I/O streams  Opening a text file for reading  Closing a stream  Reading a text file  Writing and appending to a text file.
COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
String Tokenization What is String Tokenization?
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 29, 2005.
23-Jun-15 Strings, Etc. Part I: String s. 2 About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects,
StringBuffer class  Alternative to String class  Can be used wherever a string is used  More flexible than String  Has three constructors and more.
1 StringTokenizer and StringBuffer classes Overview l StringTokenizer class l Some StringTokenizer methods l StringTokenizer examples l StringBuffer class.
Strings, Etc. Part I: Strings. About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects, have a defined.
Chapter 91 Streams and File I/O Chapter 9. 2 Reminders Project 6 released: due Nov 10:30 pm Project 4 regrades due by midnight tonight Discussion.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Console Input Using the Scanner Class Starting with version 5.0, Java includes a class for doing.
Files in Python Input techniques. Input from a file The type of data you will get from a file is always string or a list of strings. There are two ways.
1 Infinite Loops  The body of a while loop eventually must make the condition false  If not, it is an infinite loop, which will execute until the user.
1 Scanning Tokens. 2 Tokens When a Scanner reads input, it separates it into “tokens”  … at least when using methods like nextInt()  nextInt() takes.
Chapter 2 Section 2.2 Console Input Using The Scanner CLASS Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska.
Streams and File I/O Chapter 14. I/O Overview I/O = Input/Output In this context it is input to and output from programs Input can be from keyboard or.
Stream: an object that either delivers data to its destination (screen, file, etc.) or that takes data from a source (keyboard, file, etc.) –it acts as.
Session 05 Java Strings and Files. Exercise Complete the “quick-and-dirty” class CharacterCounter containing only a main() method that displays the number.
Based on OOP with Java, by David J. Barnes Input-Output1 The java.io Package 4 Text files Reader and Writer classes 4 Byte stream files InputStream, FileInputStream,
OOP with Java, David J. Barnes Input-Output1 A complex issue in programming language design. The interface to the outside world. –Differences must be accommodated.
Chapter 7 Strings  Use the String class to process fixed strings.  Use the StringBuffer class to process flexible strings.  Use the StringTokenizer.
Computer Programming 2 Lecture 5: String Processing Part b: Class StringTokenizer Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION.
Chapter 9Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 9 l Streams and Simple File I/O l Exception Handling with File.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
Chapter 9-Text File I/O. Overview n Text File I/O and Streams n Writing to a file. n Reading from a file. n Parsing and tokenizing. n Random Access n.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
1 StringTokenization Overview l StringTokenizer class l Some StringTokenizer methods l StringTokenizer examples.
Textbook: Data Structures and the Java Collections Framework 3rd Edition by William Collins William Collins.
Objectives ► Become familiar with the class String ► ► Learn how to use input and output dialog boxes in a program ► ► Learn how to tokenize the input.
Tokenizers 29-Nov-15. Tokens A tokenizer is a program that extracts tokens from an input stream A token is a “word” or a significant punctuation mark.
String Handling StringBuffer class character class StringTokenizer class.
Geoff Holmes Date Math Weighted Distr Strings String methods Tokenizers System Examples Utility Classes (Chapter 17) import java.util.*;
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
A stream is a sequence of data. A stream is a flowing sequence of characters.
CS101 Lab “File input/Output”. File input, output File : binary file, text file READ/WRITE class of “text file” - File Reading class : FileReader, BufferedReader.
Files and Streams CS /02/05 L7: Files Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
Streams and File I/O Chapter 9. Outline Overview of Streams and File I/O Text-File I/O Using the File Class Basic Binary-File I/O Object I/O with Object.
Lecture 5 I/O and Parsing
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2014 Illinois Institute of Technology- George Koutsogiannakis 1.
Chapter 9 1 Chapter 9 – Part 2 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
19-Dec-15 Tokenizers. Tokens A tokenizer is a program that extracts tokens from an input stream A token has two parts: Its value—this is just the characters.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
Keyboard and Screen I/O (Input/Output). Screen Output  System.out is an object that is part of the Java language. (Don’t worry yet about why there is.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Scanner Review Java Foundations: Introduction to Programming and Data Structures.
Chapter 9Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Announcements/Reminders l Project 6 due on Thursday March 31 (3 weeks)
1 Input-Output A complex issue in programming language design. The interface to the outside world. –Differences must be accommodated as transparently as.
CHAPTER 5 JAVA FILE INPUT/OUTPUT
Streams and File I/O Chapter 9 Chapter 9.
MSIS 655 Advanced Business Applications Programming
Streams and File I/O Chapter 14.
18 File i/o, Parsing.
Computer Programming with JAVA
OBJECT ORIENTED PROGRAMMING I LECTURE 9 GEORGE KOUTSOGIANNAKIS
Tokenizers 25-Feb-19.
Tokenizers 26-Apr-19.
Tokenizers 3-May-19.
File Input and Output.
Presentation 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 Lisa Jackson 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. 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 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 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 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 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

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 … 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(); }