import java.io.*; import java.util.Scanner; class StudentRecord { String ID; String name; String address; String level; } public class LookupStuFile2 { public static void main(String[] args) throws IOException { Scanner in = new Scanner(System.in); DataFile myFile = new DataFile("student-file"); // Open student-file StudentRecord x = new StudentRecord(); // Allocate buffer System.out.println("\nStudent file content: "); while ( true ) { try { x.ID = myFile.ReadString(6); x.name = myFile.ReadString(20); x.address = myFile.ReadString(20); x.level = myFile.ReadString(4); System.out.println(" " + x.ID + ", " + x.name + ", " + x.address + ", " + x.level); } catch ( IOException e ) { System.out.println("\n"); break; // Read error: no more data !!! } } while ( true ) { String s; System.out.print("Enter ID that you want to find: "); s = in.nextLine(); myFile.rewind(); while ( true ) { try { x.ID = myFile.ReadString(10); x.name = myFile.ReadString(30); x.address = myFile.ReadString(14); x.level = myFile.ReadString(4); /* System.out.println(x.ID.length() + " | " + s.length() ); System.out.println(">> x.ID = `" + x.ID + "', " + "s = `" + s + "'"); */ if ( s.equals(x.ID) ) { System.out.println(">> Found: " + x.ID + ", " + x.name + ", " + x.address + ", " + x.level + "\n"); break; } } catch ( IOException e ) { System.out.println("Not found !\n"); break; // Read error: no more data !!! } } } } }