1. Write a program to echo all command-line arguments to System.out, i.e.,
$ java EchoAll This is a long line
args[0] is This
args[1] is is
args[2] is a
args[3] is long
args[4] is line
1. Complete 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
public class CharacterCounter { public static void main( String[] args ) { int characterCount = 0 ; } // end main } // end class CharacterCounter
2. How would file I/O help improve the capabilities of the MemoPadApp?
// File: Echo.java - Echoes all the words in one file to an output file, one per line. 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
3. Add code to Echo.java to 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
lines = 4792 words = 32889 characters = 130156