session 05 java strings and files. exercise complete the “quick-and-dirty” class...

20
Session 05 Java Strings and Files

Upload: kerry-carpenter

Post on 12-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Session 05 Java Strings and Files. Exercise Complete the “quick-and-dirty” class CharacterCounter containing only a main() method that displays the number

Session 05

Java Strings and Files

Page 2: Session 05 Java Strings and Files. Exercise Complete the “quick-and-dirty” class CharacterCounter containing only a main() method that displays the number

ExerciseComplete the “quick-and-dirty” class CharacterCounter

containing only a main() method that displays the number of non-space characters on the command line after the command. For example:

$ java CharacterCounter

0

$ java CharacterCounter a

1

$ java CharacterCounter a bc def ghij

10

Page 3: Session 05 Java Strings and Files. Exercise Complete the “quick-and-dirty” class CharacterCounter containing only a main() method that displays the number

CharacterCount template

public class CharacterCounter {

public static void main( String[] args ) {

int characterCount = 0 ;

} // end main

} // end class CharacterCounter

Page 4: Session 05 Java Strings and Files. Exercise Complete the “quick-and-dirty” class CharacterCounter containing only a main() method that displays the number

StringTokenizer

• Useful tool for processing a String object

• Allows you to sequentially walk down a String and extract “words”/tokens that are delimited by specified characters

• What delimiter normally aids us in parsing a long string into words?

Page 5: Session 05 Java Strings and Files. Exercise Complete the “quick-and-dirty” class CharacterCounter containing only a main() method that displays the number

StringTokenizer

General usage of a StringTokenizer:

– create one using a constructor that takes a string argument to process

– send one of two messages: hasMoreTokens() and nextToken

– use a stereotypical loop to process a sequence of strings

A default StringTokenizer uses spaces as delimiters.

Page 6: Session 05 Java Strings and Files. Exercise Complete the “quick-and-dirty” class CharacterCounter containing only a main() method that displays the number

StringTokenizer Exampleimport java.util.StringTokenizer;

public class EchoWordsInArgumentV1 {

public static void main( String[] args ) {

StringTokenizer words = new StringTokenizer(args[0]);

while( words.hasMoreElements() ) {

String word = words.nextToken();

System.out.println( word );

} // end while

} // end main

} // end class EchoWordsInArgumentV1

Page 7: Session 05 Java Strings and Files. Exercise Complete the “quick-and-dirty” class CharacterCounter containing only a main() method that displays the number

StringTokenizer Example$ java EchoWordsInArgumentV1 "StringTokenizer, please process

me."

StringTokenizer,

please

process

me.

• Notice the quotes (“”) in the command line so the whole string is read as args[0].

• The comma (“,”) and period (“.”)are part of the words and not delimiters by default.

Page 8: Session 05 Java Strings and Files. Exercise Complete the “quick-and-dirty” class CharacterCounter containing only a main() method that displays the number

StringTokenizer Example 2

• Fortunately, we can construct a StringTokenizer that uses specified characters for delimiters.

• The designer of the StringTokenizer was planning ahead for future usage!!!

$ java EchoWordsInArgumentV2 "StringTokenizer, please process me."

StringTokenizer

please

process

me

Page 9: Session 05 Java Strings and Files. Exercise Complete the “quick-and-dirty” class CharacterCounter containing only a main() method that displays the number

StringTokenizer Example 2import java.util.StringTokenizer;

public class EchoWordsInArgumentV2 {

public static void main( String[] args ) {

String delimiters = " .?!()[]{}|?/&\\,;:-\'\"\t\n\r";

StringTokenizer words = new StringTokenizer( args[0], delimiters );

while( words.hasMoreElements() ) {

String word = words.nextToken();

System.out.println( word );

} // end while

} // end main

} // end class EchoWordsInArgumentV2

Page 10: Session 05 Java Strings and Files. Exercise Complete the “quick-and-dirty” class CharacterCounter containing only a main() method that displays the number

UNIX/Linux pipe

• “|” character on the command line

• Allows the output of one program to be sent as input to another program, like the UNIX “sort” utility.

$ java EchoWordsInArgumentV2 "StringTokenizer, please process me.” | sort

StringTokenizer

me

please

process

• Is this sorted? How can we fix this?

Page 11: Session 05 Java Strings and Files. Exercise Complete the “quick-and-dirty” class CharacterCounter containing only a main() method that displays the number

StringTokenizer Example 3import java.util.StringTokenizer;

public class EchoWordsInArgumentV3 {

public static void main( String[] args ) {

String delimiters = " .?!()[]{}|?/&\\,;:-\'\"\t\n\r";

StringTokenizer words = new StringTokenizer( args[0],

delimiters );

while( words.hasMoreElements() ) {

String word = words.nextToken();

word = word.toLowerCase();

System.out.println( word );

} // end while

} // end main

} // end class EchoWordsInArgumentV3

Page 12: Session 05 Java Strings and Files. Exercise Complete the “quick-and-dirty” class CharacterCounter containing only a main() method that displays the number

StringTokenizer Example 3$ java EchoWordsInArgumentV3 "StringTokenizer, please process me." | sort

me

please

process

stringtokenizer

Page 13: Session 05 Java Strings and Files. Exercise Complete the “quick-and-dirty” class CharacterCounter containing only a main() method that displays the number

Java File I/O

• Allows us to write and read “permanent” information to and from disk

• How would file I/O help improve the capabilities of the MemoPadApp?

Page 14: Session 05 Java Strings and Files. Exercise Complete the “quick-and-dirty” class CharacterCounter containing only a main() method that displays the number

Java File I/O Example: Echo.java• echoes all the words in one file to an output

file, one per line.

$ java Echo hamlet.txt hamlet.out

$ less hamlet.out

1604

the

tragedy

of

hamlet

prince

of

denmark

by

william

shakespeare ...

Page 15: Session 05 Java Strings and Files. Exercise Complete the “quick-and-dirty” class CharacterCounter containing only a main() method that displays the number

Study Echo.java’s File I/O

• have constructors that allow convenient and flexible processing

• send input message: readLine()

• send output messages: print() and println()

• use a stereotypical loop to process a file of lines

• use of the stereotypical StringTokenizer loop as inner loop

Page 16: Session 05 Java Strings and Files. Exercise Complete the “quick-and-dirty” class CharacterCounter containing only a main() method that displays the number

import java.io.*;import java.util.StringTokenizer;

public class Echo { public static void main( String[] args ) throws IOException { String delimiters = " .?!()[]{}|?/&\\,;:-\'\"\t\n\r";

BufferedReader inputFile = new BufferedReader(new FileReader(args[0]) ); PrintWriter outputFile = new PrintWriter( new FileWriter( args[1] ) );

String buffer = null;

while( true ) { buffer = inputFile.readLine();

if ( buffer == null ) break;

buffer = buffer.toLowerCase(); StringTokenizer tokens = new StringTokenizer( buffer, delimiters );

while( tokens.hasMoreElements() ) { String word = tokens.nextToken(); outputFile.println( word ); } // end while } // end while(true)... } // end main} // end class Echo

Page 17: Session 05 Java Strings and Files. Exercise Complete the “quick-and-dirty” class CharacterCounter containing only a main() method that displays the number

wc - UNIX/Linux utility

• wc prints the number of lines, words, and characters in a file to standard output.

• For example:

$ wc hamlet.txt

4792 31957 196505 hamlet.txt

Page 18: Session 05 Java Strings and Files. Exercise Complete the “quick-and-dirty” class CharacterCounter containing only a main() method that displays the number

Exercise

• Using Echo.java as your starting point, create a WordCount.java program that does the same thing as wc, i.e., prints the number of lines, words, and characters in a file to standard output. For example:

$ java WordCount hamlet.txt

4792 32889 130156

Page 19: Session 05 Java Strings and Files. Exercise Complete the “quick-and-dirty” class CharacterCounter containing only a main() method that displays the number

import java.io.*;import java.util.StringTokenizer;

public class WordCount { public static void main( String[] args ) throws IOException { String delimiters = " .?!()[]{}|?/&\\,;:-\'\"\t\n\r";

BufferedReader inputFile = new BufferedReader( new FileReader( args[0] ) );

String buffer = null; int chars = 0; int words = 0; int lines = 0;

while( true ) { buffer = inputFile.readLine();

if ( buffer == null ) break;

lines++;

buffer = buffer.toLowerCase(); StringTokenizer tokens = new StringTokenizer( buffer, delimiters );

while( tokens.hasMoreElements() ) { String word = tokens.nextToken(); words++; chars += word.length(); } // end while } // end while( true )...

System.out.println( "" + lines + " " + words + " " + chars ); } // end main} // end class WordCount

Page 20: Session 05 Java Strings and Files. Exercise Complete the “quick-and-dirty” class CharacterCounter containing only a main() method that displays the number

Why the difference in the number of words and number of characters?

$ wc hamlet.txt

4792 31957 196505 hamlet.txt

$ java WordCount hamlet.txt

4792 32889 130156