public static Person searchFileSequentially( String fileName, String desiredName) { // -------------------------------------------------------- // Searches a text file sequentially for a desired person. // Precondition: fileName is the name of a text file of // names and data about people. Each person is represented // by two lines in the file: The first line contains the // person's name, and the second line contains the person's // salary. desiredName is the name of the person sought. // Postcondition: If desiredName was found in the file, // a Person object that contains the person's // name and data is returned. Otherwise, the value null // is returned to indicate that the desiredName was not // found. The file is unchanged and closed. // -------------------------------------------------------- BufferedReader ifStream = null; try { ifStream = new BufferedReader(new FileReader(fileName)); String nextName = null; String nextSalary = null; boolean found = false; while (!found && (nextName = ifStream.readLine()) != null) { nextSalary = ifStream.readLine(); if (nextName.compareTo(desiredName) == 0) { found = true; } // end if } // end while if (found) { return new Person(nextName, Double.parseDouble(nextSalary)); } else { return null; } // end if } // end try catch (IOException e) { System.out.println("Error processing file"); return null; } // end catch finally { if (ifStream != null) { try { ifStream.close(); // close the file } catch (IOException e) { System.out.println("Error closing file..."); } } // end if } // end finally } // end searchFileSequentially