import java.io.IOException; import java.util.ArrayList; import javax.swing.JOptionPane; import javax.swing.JTextArea; import javax.swing.JScrollPane; import java.awt.Font; import java.io.File; import java.util.Scanner; public class WordsExerciseSolution { public WordsExerciseSolution() throws IOException { ArrayList theWords = new ArrayList(); // modify code here to handle the case when the user clicks cancel String fileName = JOptionPane.showInputDialog("Enter Input File Name","src/WordsExercise.java"); if( fileName == null ) return; Scanner fileScanner = new Scanner(new File(fileName)); // the delimiters are any number of spaces,commas,periods or end-of-lines fileScanner.useDelimiter("[ ,.\n]+"); while( fileScanner.hasNext() ) { // words here are any characters except space, '.', '.' and end-of-line // for the assignment words consist only of letters String nextWord = fileScanner.next(); // add code here to add the next word to // the ArrayList theWords if not already there if( !theWords.contains(nextWord) ) theWords.add(nextWord); } // end while String out = "The unique \"words\" are:\n\n"; // iterate through theWords and concatenate each word to // out along with an end-of-line character for( String word : theWords ) { out += word + "\n"; }// end for JTextArea outArea = new JTextArea(out,20,35); outArea.setFont(new Font("Courier",Font.PLAIN,15)); JOptionPane.showMessageDialog(null,new JScrollPane(outArea)); } // end WordExercise() public static void main(String[] a) throws IOException { new WordsExerciseSolution(); System.exit(0); } // end main } // end WordsExercise