Tirgul no. 13 - PowerPoint PPT Presentation

About This Presentation
Title:

Tirgul no. 13

Description:

'I have a car' 3. Parsing Strings (contd.) We can define more than one Delimeter. ... We will now design new Filters for reading and writing which can Translate (or ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 17
Provided by: csHu
Category:
Tags: tirgul

less

Transcript and Presenter's Notes

Title: Tirgul no. 13


1
Tirgul no. 13
  • Topics covered
  • String parsing.
  • Text file I/O.
  • Extending Filters.

2
Parsing Strings
  • Many different tasks involve taking a string and
    parsing it into Tokens.
  • A Token is a part of the string.
  • Tokens are separated by Delimeters.
  • a Delimeter can be any char.
  • Examples space, \n
  • Tokens may be separated by many delimeters
  • I have a car

3
Parsing Strings (contd.)
  • We can define more than one Delimeter.
    For example many times
    the Delimiters may be all White Space
    characters space \n \t \r
  • The package java.util includes a class called
    StringTokenizer that makes it easy to parse a
    string into tokens.
  • The StringTokenizers default delimiter list is
    all whitespace characters.
  • The simple constructor receives the String to be
    parsed
  • StringTokenizer tokenizer
  • tokenizer new StringTokenizer(sentence)

4
StringTokenizer
  • The method nextToken reads the next Token from
    the String and returns it as a String
  • String word tokenizer.nextToken()
  • All delimeter characters (by default whitespace
    chars) are ignored. The word returned is a
    sequence of all non delimiter characters from the
    current position up to the first delimiter
    char.

5
StringTokenizer (contd.)
  • For example consider the sentence
  • This is a lovely day
  • to go out
  • Assuming that the delimiters are whitespace chars
    this sentence contains 8 Tokens
    This,is,a,lovely,day,to,go,out. All other chars
    in the sentence are whitespace and the
    StringTokenizer will not include these in the
    tokens that it returns.

6
StringTokenizer Example
import java.util. //so we can use the
StringTokenizer class public class
SentenceParser public static void
main(String args) System.out.print(Plea
se enter a sentence ) String sentence
EasyInput.readString()
//init a StringTokenizer object that we will use
//to parse the
sentence. StringTokenizer tokenizer
new StringTokenizer(sentence) while(
tokenizer.hasMoreTokens() ) String
word tokenizer.nextToken() System.out.println(w
ord)
7
Reading Text from Files
BufferedReader Requires the following import
import java.io.BufferedReader
or
import
java.io. Construction public
BufferedReader(Reader in)
Create a buffering character-input stream that
uses a default-sized input
buffer. Line Reading public String
readLine() throws IOException
Read a line of text. A line is considered to be
terminated by any one of a line feed
('\n'), a carriage return ('\r'), or a carriage
return followed immediately by a linefeed.
Returns a String containing the contents of
the line, not including any
line-termination characters, or null if the end
of the stream has been reached.
8
Reading Text from Files (contd.)
FileReader Requires the following import
import java.io.FileReader
or
import java.io. Constructi
on public FileReader(String fileName)
throws FileNotFoundException
Creates a new FileReader, given the name of the
file to read from.
9
Reading Text from Files Example
import java.io. import java.util.StringTokenizer
try int lineNum, wordNum String
line BufferedReader inStream new
BufferedReader(new FileReader(args0))
lineNum wordNum 0 do line
inStream.readLine() if(line ! null)
lineNum StringTokenizer
st new StringTokenizer(line,args1)
wordNum st.countTokens()
while(line ! null) System.out.println("Th
ere are " lineNum " lines.")
System.out.println("There are " wordNum "
words.") catch(FileNotFoundException fnfe)
System.out.println("File (" args0 ") not
found.") catch(IOException ioe)
System.out.println("I/O error while reading file
(" args0 ")")
10
Extending Filter Classes
  • FilterReader and FilterWriter are filter streams
    that can be extended to provide new formatting of
    textual data.
  • Recall that a FilterStream is a stream that
    connects to other Streams and therefore can be
    connected to any Reader/Writer.
  • We will now design new Filters for reading and
    writing which can Translate (or encode/decode)
    strings.

11
TranslateWriter
  • import java.io.
  • public class TranslateWriter extends
    FilterWriter
  • private String from
  • private String to
  • private static char oneChar
  • private static char charArray
  • static
  • oneChar new char1
  • public TranslateWriter(Writer w, String from,
    String to)
  • super(w)
  • this.from from
  • this.to to

12
TranslateWriter (cont.)
  • public void write(int c) throws IOException
  • oneChar0 (char)c
  • write(oneChar,0,1)
  • public void write(char cbuf, int off, int len)
    throws IOException
  • if(cbuf null)
  • throw new IOException()
  • int i
  • for(i0 iltcbuf.length i)
  • int mapIndex from.indexOf(cbufi)
  • if(mapIndex gt0)
  • cbufi to.charAt(mapIndex)
  • super.write(cbuf,off,len)

13
TranslateWriter (main)
  • public static void main(String args)
  • OutputStreamWriter osw
  • TranslateWriter translateWriter
  • osw new OutputStreamWriter(System.out)
  • translateWriter new TranslateWriter(osw)
  • try
  • translateWriter.write("Java is Great")
  • translateWriter.flush()
  • catch(IOException ioe)
  • System.err.println(ioe)
  • System.exit(1)

14
TranslateReader
  • import java.io.
  • public class TranslateReader extends
    FilterReader
  • private String from
  • private String to
  • private static char oneChar
  • static
  • oneChar new char1
  • public TranslateReader(Reader r, String from,
    String to)
  • super(r)
  • this.from from
  • this.to to
  • public TranslateReader(Reader r)

15
TranslateReader cont.
  • public int read() throws IOException
  • int result read(oneChar,0,1)
  • if(result lt 0)
  • return(result)
  • else
  • return(oneChar0)
  • public int read(char cbuf, int off, int len)
    throws IOException
  • int n super.read(cbuf,off,len)
  • int i
  • for(i0 iltn i)
  • int mapIndex from.indexOf(cbufi)
  • if(mapIndex gt0)
  • cbufi to.charAt(mapIndex)
  • return(n)

16
TranslateReader (main)
  • public static void main(String args)
  • StringReader sr new StringReader("Java is
    Great")
  • TranslateReader translateReader new
    TranslateReader(sr)
  • LineNumberReader lnr new LineNumberReader(trans
    lateReader)
  • try
  • String line lnr.readLine()
  • System.out.println(line)
  • catch(IOException ioe)
  • //end of TranslateReader class.
Write a Comment
User Comments (0)
About PowerShow.com