Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Tirgul no. 13 Topics covered: H String parsing. H Text file I/O. H Extending Filters."— Presentation transcript:

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

2 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 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 StringTokenizer’s default delimiter list is all whitespace characters.  The simple constructor receives the String to be parsed: StringTokenizer tokenizer; tokenizer = new StringTokenizer(sentence);

4 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 5 StringTokenizer (contd.)  For example consider the sentence: “This is a lovelyday 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 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(“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); }

7 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 8 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.

9 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(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] +")"); }

10 10 Extending Filter Classes H FilterReader and FilterWriter are filter streams that can be extended to provide new formatting of textual data. H Recall that a FilterStream is a stream that connects to other Streams and therefore can be connected to any Reader/Writer. H We will now design new Filters for reading and writing which can Translate (or encode/decode) strings.

11 11TranslateWriter 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; }

12 12 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); }

13 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 14TranslateReader 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; }

15 15 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); }

16 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(translateReader); try{ String line= lnr.readLine(); System.out.println(line); }catch(IOException ioe){ } }//end of TranslateReader class.


Download ppt "1 Tirgul no. 13 Topics covered: H String parsing. H Text file I/O. H Extending Filters."

Similar presentations


Ads by Google