String Tokenization What is String Tokenization?

Slides:



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

Lecture 15: I/O and Parsing
More Java Syntax. Scanner class Revisited The Scanner class is a class in java.util, which allows the user to read values of various types. There are.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 1 Chapter 6: Iteration 1 Chapter 6 Iteration.
Chapter 3, More on Classes & Methods Clark Savage Turner, J.D., Ph.D. Copyright 2003 CSTurner, from notes and text.
Using Processing Stream. Predefined Streams System.in InputStream used to read bytes from the keyboard System.out PrintStream used to write bytes to the.
Unit 201 FILE IO Types of files Opening a text file for reading Reading from a text file Opening a text file for writing/appending Writing/appending to.
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.
Exception examples. import java.io.*; import java.util.*; class IO { private String line; private StringTokenizer tokenizer; public void newline(DataInputStream.
1 LECTURE#7: Console Input Overview l Introduction to Wrapper classes. l Introduction to Exceptions (Java run-time errors). l Console input using the BufferedReader.
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.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 29, 2005.
1 Tirgul no. 13 Topics covered: H String parsing. H Text file I/O. H Extending Filters.
StringBuffer class  Alternative to String class  Can be used wherever a string is used  More flexible than String  Has three constructors and more.
1 Text File I/O Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l.
1 StringTokenizer and StringBuffer classes Overview l StringTokenizer class l Some StringTokenizer methods l StringTokenizer examples l StringBuffer class.
1 Introduction to Console Input  Primitive Type Wrapper Classes  Converting Strings to Numbers  System.in Stream  Wrapping System.in in a Buffered.
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.
1 Streams Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l Writing.
Strings and File I/O. Strings Java String objects are immutable Common methods include: –boolean equalsIgnoreCase(String str) –String toLowerCase() –String.
1 Repetition structures [cont’d] Overview l Nested Loops l Sentinel-Controlled loop l Avoiding Number Format exception.
1 One-Dimensional Arrays  What are and Why 1-D arrays?  1-D Array Declaration  Accessing elements of a 1-D Array  Initializer List  Passing Array.
Two Ways to Store Data in a File Text format Binary format.
Chapter 10 Exceptions and File I/O. © 2004 Pearson Addison-Wesley. All rights reserved10-2 Exceptions Exception handling is an important aspect of object-oriented.
Session 05 Java Strings and Files. Exercise Complete the “quick-and-dirty” class CharacterCounter containing only a main() method that displays the number.
1 Java Console I/O Introduction. 2 Java I/O You may have noticed that all the I/O that we have done has been output The reasons –Java I/O is based on.
Console Input. So far… All the inputs for our programs have been hard-coded in the main method or inputted using the dialog boxes of BlueJ It’s time to.
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.
I/O in Java Dennis Burford
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.
1 Recitation 8. 2 Outline Goals of this recitation: 1.Learn about loading files 2.Learn about command line arguments 3.Review of Exceptions.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
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.
1 StringTokenization Overview l StringTokenizer class l Some StringTokenizer methods l StringTokenizer examples.
Two Ways to Store Data in a File  Text format  Binary format.
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.
Java 1.5 The New Java Mike Orsega Central Carolina CC.
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.
CS101 Lab “File input/Output”. File input, output File : binary file, text file READ/WRITE class of “text file” - File Reading class : FileReader, BufferedReader.
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.
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.
Lecture 5 I/O and Parsing
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 11 GEORGE KOUTSOGIANNAKIS Copyright: 2015 / Illinois Institute of Technology/George Koutsogiannakis 1.
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.
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
Object-Oriented Programming (Java) Review Unit 1 Class Design Basic Console I/O StringTokenizer Exception UML class diagram.
Java Input/Output. Java Input/output Input is any information that is needed by your program to complete its execution. Output is any information that.
Strings and File I/O. Strings Java String objects are immutable Common methods include: –boolean equalsIgnoreCase(String str) –String toLowerCase() –String.
Chapter 9Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Announcements/Reminders l Project 6 due on Thursday March 31 (3 weeks)
IAS 1313: OBJECT ORIENTED PROGRAMMING Week 3: Data Type, Control Structure and Array Prepared by: Mrs Sivabalan1.
1 Input-Output A complex issue in programming language design. The interface to the outside world. –Differences must be accommodated as transparently as.
Introduction to programming in java
Strings.
Strings and File I/O.
CHAPTER 5 JAVA FILE INPUT/OUTPUT
Reading and Writing Text Files
OBJECT ORIENTED PROGRAMMING II LECTURE 13_1 GEORGE KOUTSOGIANNAKIS
OBJECT ORIENTED PROGRAMMING I LECTURE 9 GEORGE KOUTSOGIANNAKIS
OBJECT ORIENTED PROGRAMMING II LECTURE 20 GEORGE KOUTSOGIANNAKIS
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Presentation transcript:

String Tokenization What is String Tokenization? The StringTokenizer class Examples

What is String Tokenization? So far we have been reading our input one value at a time. Sometimes it is more natural to read a group of input at a time. For example, when reading records of students from a text file, it is natural to read a whole record at a time. "995432 Al-Suhaim Adil 3.5" The readLine() method of the Buffered Reader class can read a group of input as a single string object. The problem is, how do we break this string object into individual words known as tokens? "995432" "Al-Suhaim Adil” "3.5“ This process is what String tokenization is about.

The StringTokenizer Class The StringTokenizer class, of the java.util package, is used to break a string object into individual tokens. It has the following constructors: Constructor function StringTokenizer(String str) Creates a StringTokenizer object that uses white space characters as delimiters. StringTokenizer(String str, String delimiters) Creates a StringTokenizer object that uses the characters in delimiters as separators. StringTokenizer(String str,String delimiters,boolean returnTokens) Creates a StringTokenizer object that uses characters in delimiters as separators and treats separators as tokens.

StringTokenizer Methods The following are the main methods of the StringTokenizer class: Method function String nextToken() throws NoSuchElementException Returns the next token as a string from this StringTokenizer object. Throws an exception if there are no more tokens. int countTokens() Returns the count of tokens in this StringTokenizer object that are not yet processed by nextToken() -- initially all. boolean hasMoreTokens() Returns true if there are more tokens not yet processed by nextToken().

How to apply the methods To break a string into tokens, first, a StringTokenizer object is created. String myString = "I like Java very much"; StringTokenizer tokenizer = new StringTokenizer(myString); Then any of the following loops can be used to process the tokens: while(tokenizer.hasMoreTokens()){ String token = tokenizer.nextToken(); // process token } or int tokenCount = tokenizer.countTokens(); for(int k = 1; k <= tokenCount; k++){

Example 1 The following program reads grades from the keyboard and finds the average. The grades are read in one line. import java.io.*; import java.util.StringTokenizer; public class TokenizerExamplel{ public static void main(String[] args)throws IOException{ BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter grades in one line:"); String inputLine = stdin.readLine(); StringTokenizer tokenizer = new StringTokenizer(inputLine); int count = tokenizer.countTokens(); double sum = 0; while(tokenizer.hasMoreTokens()) sum += Double.parseDouble(tokenizer.nextToken()); System.out.println("\nThe average = "+ sum / count); }

Example 2 Output: Hi there do you like Java I very much This example shows how to use the second constructor of StringTokenizer class. It tokenizes the words in a string, such that the punctuation characters following the words are not appended to the resulting tokens. import java.util.StringTokenizer; public class TokenizerExample2{ public static void main(String[] args){ String inputLine = "Hi there, do you like Java? I do;very much."; StringTokenizer tokenizer = new StringTokenizer (inputLine, ",.?;:! \t\r\n"); while(tokenizer.hasMoreTokens()) System.out.println(tokenizer.nextToken()); } Output: Hi there do you like Java I very much

Example 3 This example shows how to use the third constructor of StringTokenizer class. It tokenizes an arithmetic expression based on the operators and returns both the operands and the operators as tokens. import java.util.StringTokenizer; public class TokenizerExample3{ public static void main(String[] args){ String inputLine = "(2+5)/(10-1)"; StringTokenizer tokenizer = new StringTokenizer(inputLine,“+—*/()",true); while(tokenizer.hasMoreTokens()) System.out.println(tokenizer.nextToken()); } Output: ( 2 + 5 ) / 10 - 1