import java.awt.Font; import java.io.File; import java.util.ArrayList; import java.util.Scanner; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JTextArea; /* * CountWordsExercise.java * * Created on October 2, 2005, 2:24 PM * */ /** * * @author Bill Kraynek */ public class CountWordsExercise { /** Creates a new instance of CountWordsExercise */ // must announce that file exceptions will not be caught public CountWordsExercise() throws Exception { String fileName = JOptionPane.showInputDialog("Enter file name","src/CountWordsExercise.java"); Scanner fileScanner = new Scanner(new File(fileName)); // set up delimiters to be anything except a letter fileScanner.useDelimiter("put your code for regular expression here"); ArrayList words = new ArrayList(); ArrayList counts = new ArrayList(); while( fileScanner.hasNext() ) { // put your code here to get next word // add word to words and 1 to counts if a new word // update counts if word is a repeat word }// end while JTextArea outArea = new JTextArea(30,40); // need a monospaced font for columns to line up Font myFont = new Font("monospaced", Font.BOLD, 20); outArea.setFont(myFont); String out = ""; // in out put a heading // then on each line display a word followed its count // make sure that the words and counts line up in he same column // for example // import 7 // java 6 // etc outArea.setText(out); JOptionPane.showMessageDialog(null, new JScrollPane(outArea)); } /** * @param args the command line arguments */ // must announce that file exceptions will not be caught public static void main(String[] args) throws Exception { new CountWordsExercise(); } }