void setup() { // try to do some work, but be ready for possible errors try { int i; String justReadLine; // create a size zero array (as we don't know how big we will eventually need it) String[] studentNamesArray = new String[0]; // open the file of interest BufferedReader reader = createReader("C:\\Users\\WFUT4002009\\Documents\\Teaching\\CSC111\\Fall2010\\lectures\\11172010\\Files\\studentNames.txt"); // while there are more lines in the file justReadLine = reader.readLine(); while (justReadLine != null) { // add it to the back of our array using the append array function studentNamesArray = append(studentNamesArray,justReadLine); justReadLine = reader.readLine(); } // done reading? close the scanner/file reader.close(); int numberOfStudents = studentNamesArray.length; println("Read in " + numberOfStudents + " student names"); // print out names to screen for (i = 0; i < numberOfStudents; i++) { println(studentNamesArray[i]); } String[] sortedNamesArray = sort(studentNamesArray); // now write them back to a file // create a PrintWriter to allow us to use println to print to a file PrintWriter printWriter = createWriter("sortedNames3.txt"); // write out each entry from the array on its own line for (i = 0; i < numberOfStudents; i++) { // write to the screen, then to the file println(sortedNamesArray[i]); printWriter.println(sortedNamesArray[i]); } // flush the printWriter (write anything not yet written!) printWriter.flush(); // close/release the file printWriter.close(); } catch (Exception exc) { // if an error occurs, recognize it and print it to the screen println(exc); } }