package studentDB; import java.io.*; import java.util.*; /** * Helper class used to read files containing student * data bases. This class assumes that student data bases * are text files containing lines of the following form: * *
line ::= {ugStudent} {gradStudent} {course}
 *	ugStudent ::= "undergrad" "+" name "+" studentNumber
 *	gradStudent ::= "grad" "+" name "+" studentNumber "+" thesisTitle
 *	course ::= "course" "+" title { "+" studentNumber }
*/ public class StudentDBFile { // The file we're reading from is represented as // a buffered reader. BufferedReader db; /** * Opens a student data base located in the given file. */ public StudentDBFile (String fileName) { try { db = new BufferedReader (new FileReader (fileName)); } catch (FileNotFoundException e) { System.out.println ("File " + fileName + " does not exist"); System.exit (1); } } /** * Retrieves the next line of the student data base, split into * tokens. For example, the line: * * undergrad+Keith Richards+123 * * would be returned as an array of three strings: * * ["undergrad", "Keith Richards", "123"] * * If the file has been completely read, then null is returned. */ public String[] getNextLine () { String line = null; try { line = db.readLine(); } catch (IOException e) { System.out.println ("Error reading from data base"); } // If we have reached end of file, return null if (line == null) return null; // Break the line into tokens // We create a string tokenizer to deliver the tokens, // and extract the tokens into an array. StringTokenizer s = new StringTokenizer (line, "+"); String[] tokens = new String [s.countTokens()]; for (int i=0; i