import java.io.File; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JTextArea; import java.io.IOException; import java.util.Scanner; // This example illustrates opening a text file and reading lines one at a time // and then storing the words in a String array. It also shows how to have scrollable // output public class Example3A { // The throws Exception clause is need when doing file I/O public Example3A() throws IOException { String[] words = getWordArray(); String out = "The words are:\n"; for(String word : words) { out += " " + word + "\n"; } // end for // The following area will have 25 rows and 40 columns for the output String out JTextArea outArea = new JTextArea(out,30,40); JOptionPane.showMessageDialog(null,new JScrollPane(outArea)); } // end Example3A() public static void main(String[] args) throws IOException { new Example3A(); } // end main // The throws Exception clause is need when doing file I/O String[] getWordArray() throws IOException { int size = 10; String[] words = new String[size]; int total = 0; Scanner fileScanner = new Scanner(new File("src/Example3A.java")); while( fileScanner.hasNext() ) { if( total == words.length ) words = resize(words); // The default delimiters are the white space characters words[total] = fileScanner.next(); total++; } // end while String[] returnArray = new String[total]; for( int i = 0; i < total; i++ ) { returnArray[i] = words[i]; } // end for return returnArray; } // end getWordArray String[] resize(String[] s) { String[] save = s; s = new String[2*save.length]; for( int i = 0; i < save.length; i++ ) { s[i] = save[i]; } // end for return s; }// end resize } // end Example3A