Download Tirgul no. 7 - CS

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Tirgul no. 13
Topics covered:



String parsing.
Text file I/O.
Extending Filters.
1
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”
2
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 StringTokenizer’s default delimiter list is all whitespace
characters.
 The simple constructor receives the String to be parsed:
StringTokenizer tokenizer;
tokenizer = new StringTokenizer(sentence);
3
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.
4
StringTokenizer (contd.)
For example consider the sentence:
“This is a lovely
to go out
day
“
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.
5
StringTokenizer Example
import java.util.*; //so we can use the StringTokenizer class
public class SentenceParser{
public static void main(String[] args){
System.out.print(“Please 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(word);
}
}
}
6
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.
7
Reading Text from Files (contd.)
FileReader
Requires the following import:
import java.io.FileReader;
or
import java.io.*;
Construction:
public FileReader(String fileName) throws FileNotFoundException {…}
Creates a new FileReader, given the name of the file to read from.
8
Reading Text from Files Example
import java.io.*;
import java.util.StringTokenizer;
try {
int lineNum, wordNum;
String line;
BufferedReader inStream = new BufferedReader(new FileReader(args[0]));
lineNum = wordNum = 0;
do {
line = inStream.readLine();
if(line != null) {
lineNum++;
StringTokenizer st = new StringTokenizer(line,args[1]);
wordNum += st.countTokens();
}
}while(line != null);
System.out.println("There are " + lineNum + " lines.");
System.out.println("There are " + wordNum + " words.");
}catch(FileNotFoundException fnfe) {
System.out.println("File ("+ args[0] +") not found.");
}catch(IOException ioe) {
System.out.println("I/O error while reading file ("+ args[0] +")");
}
9
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.
10
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 char[1];
}
public TranslateWriter(Writer w, String from, String to){
super(w);
this.from= from;
this.to= to;
}
public TranslateWriter(Writer w){
super(w);
String upper= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String lower= "abcdefghijklmnopqrstuvwxyz";
this.from= upper + lower;
this.to= lower + upper;
}
11
TranslateWriter (cont.)
public void write(int c) throws IOException {
oneChar[0]= (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(i=0; i<cbuf.length; i++){
int mapIndex= from.indexOf(cbuf[i]);
if(mapIndex >=0)
cbuf[i]= to.charAt(mapIndex);
}
super.write(cbuf,off,len);
}
public void write(String s, int off, int len) throws IOException{
if(s == null)
throw new IOException();
charArray= s.toCharArray();
write(charArray,off,len);
}
12
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);
}
}
}
13
TranslateReader
import java.io.*;
public class TranslateReader extends FilterReader{
private String from;
private String to;
private static char[] oneChar;
static{
oneChar= new char[1];
}
public TranslateReader(Reader r, String from, String to){
super(r);
this.from= from;
this.to= to;
}
public TranslateReader(Reader r){
super(r);
String upper= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String lower= "abcdefghijklmnopqrstuvwxyz";
this.from= upper + lower;
this.to= lower + upper;
}
14
TranslateReader cont.
public int read() throws IOException {
int result= read(oneChar,0,1);
if(result < 0)
return(result);
else
return(oneChar[0]);
}
public int read(char[] cbuf, int off, int len) throws IOException{
int n= super.read(cbuf,off,len);
int i;
for(i=0; i<n; i++){
int mapIndex= from.indexOf(cbuf[i]);
if(mapIndex >=0)
cbuf[i]= to.charAt(mapIndex);
}
return(n);
}
15
TranslateReader (main)
public static void main(String[] args){
StringReader sr= new StringReader("Java is Great");
TranslateReader translateReader= new TranslateReader(sr);
LineNumberReader lnr= new LineNumberReader(translateReader);
try{
String line= lnr.readLine();
System.out.println(line);
}catch(IOException ioe){
}
}
}//end of TranslateReader class.
16