import java.io.*; import java.util.Scanner; class DataDescription { String fieldName; String fieldType; int fieldSize; } public class LookupData { static int MAXNFIELDS = 10; // Max. # fields in 1 record static int n_fields; // Actual number of fields /* ------------------------------------------------------------ (1) Variables used to store the DESCRIPTION of the data ------------------------------------------------------------ */ static DataDescription[] dataDes = new DataDescription[MAXNFIELDS]; // This is a CONSTRUCTOR method for static variables static { // Need to create objects for the dataDes[] for ( int i = 0; i < MAXNFIELDS; i++ ) { dataDes[i] = new DataDescription(); } } /* ------------------------------------------------------- (2) Variables used to store the ACTUAL data ------------------------------------------------------- */ static String[] c_buf=new String[MAXNFIELDS]; // Used to store String fields static int[] i_buf=new int[MAXNFIELDS]; // Used to store int fields public static void main(String[] args) throws IOException { Scanner in = new Scanner(System.in); /* =========================================================== We must first find out the STRUCTURE of the data file This information is stored in the data DESCRIPTION file =========================================================== */ DataFile descrFile = new DataFile("db-description"); // 1. Open the data description file /* ------------------------------------------------------- Read in the data description and store them in the DataDes[] array (define in (1)) ------------------------------------------------------- */ n_fields = 0; // Count the actual number of fields in data while ( true ) { try { dataDes[n_fields].fieldName = descrFile.ReadString(24); dataDes[n_fields].fieldType = descrFile.ReadString(4); dataDes[n_fields].fieldSize = descrFile.ReadInt(); System.out.println("Field: " + dataDes[n_fields].fieldName + ", type: " + dataDes[n_fields].fieldType + ", size: " + dataDes[n_fields].fieldSize); n_fields++; } catch ( IOException e ) { System.out.println("\nFinish reading data description file....\n"); break; // Read error: no more data !!! } } /* ============================================================ We first read the data and print them out ============================================================ */ DataFile dataFile = new DataFile("db-data"); // First open data file System.out.println( "The data file contains these records:\n"); while ( true ) { /* ============================================================ Read in all the (n_fields) fields ============================================================ */ try { for ( int i = 0; i < n_fields; i++ ) { /* ------------------------------------------------------ Read in the data depending on the TYPE of this field ------------------------------------------------------ */ if ( dataDes[i].fieldType.equals("I") ) { /* -------------------------------------------------------- Field i is an integer, use i_buf[i] to store the value -------------------------------------------------------- */ i_buf[i] = dataFile.ReadInt(); } else { /* -------------------------------------------------------- Field i is an String, use c_buf[i] to store the value -------------------------------------------------------- */ c_buf[i] = dataFile.ReadString( dataDes[i].fieldSize ); // We know the filed size !!! } } PrintRecord(); // Print the data read - see below for method } catch ( IOException e ) { System.out.print("\nDone.\n"); break; } } /* ============================================================ Now let's do some look up on the first data field ============================================================ */ int lookup_i = 0; String lookup_s = null; System.out.println( "\nLookup data:\n"); while ( true ) { System.out.print("Please enter " + dataDes[0].fieldName + " to lookup:"); if ( dataDes[0].fieldType.equals( "I" ) ) { lookup_i = in.nextInt(); in.nextLine(); // Read the newline char } else { lookup_s = in.nextLine(); } /* ============================================================ Find the value ============================================================ */ dataFile.rewind(); // Rewind data file while ( true ) { try { /* ===================================================== Read the next record (ALL fields !) ===================================================== */ for ( int i = 0; i < n_fields; i++ ) { /* ------------------------------------------------------ Read in the data depending on the TYPE of this field ------------------------------------------------------ */ if ( dataDes[i].fieldType.equals("I") ) { /* -------------------------------------------------------- Field i is an integer, use i_buf[i] to store the value -------------------------------------------------------- */ i_buf[i] = dataFile.ReadInt(); } else { /* -------------------------------------------------------- Field i is an String, use c_buf[i] to store the value -------------------------------------------------------- */ c_buf[i] = dataFile.ReadString( dataDes[i].fieldSize ); // We know the filed size !!! } } /* ===================================================== Check if the first field contains the search value ===================================================== */ if ( dataDes[0].fieldType.equals( "I" ) ) { if ( i_buf[0] == lookup_i ) { System.out.print("Found: "); PrintRecord( ); break; } } else { if ( c_buf[0].equals( lookup_s ) ) { System.out.print("Found: "); PrintRecord( ); break; } } } catch ( IOException e ) { System.out.print("Not found....\n"); break; } } } } public static void PrintRecord( ) { for ( int i = 0; i < n_fields; i++ ) { if ( dataDes[i].fieldType.equals("I") ) { /* -------------------------------------------------------- Field i is an integer, use i_buf[i] to store the value -------------------------------------------------------- */ System.out.print( i_buf[i] + " "); } else { /* -------------------------------------------------------- Field i is an String, use c_buf[i] to store the value -------------------------------------------------------- */ System.out.print( c_buf[i] + " "); } } System.out.println( ); // Print newline to separate records } }