//Agustinus Wibowo import java.io.*; import java.util.StringTokenizer; class BinarySearch { public static void main(String[] args) throws IOException { //load the data String[][] data = load(); BufferedReader r = new BufferedReader( new InputStreamReader(System.in)); while(true) { System.out.print("Enter Club name: "); String input = r.readLine(); int index = binSearch(data[0], 0, data[0].length -1, input); if ( index > -1 ) { System.out.println("ClubName:\t" + data[0][index]); System.out.println("Teacher:\t" + data[1][index]); System.out.println("Found at index: " + (index+1)); } else System.out.println("Not found"); System.out.println("\n\n"); } } private static String[][] load() { String data[][] = new String[0][0]; try { BufferedReader fin = new BufferedReader(new FileReader("Default.txt")); //get the size of the data int size = 0; String temp = fin.readLine(); while( temp != null && !temp.equals("")) { size++; temp = fin.readLine(); } //reload the file fin = new BufferedReader( new FileReader("Default.txt")); //read every line of file temp = fin.readLine(); int at = 0; //counter data = new String[2][size]; //the data to be returned while ( temp != null && !temp.equals("")) { data[0][at] = temp.substring( 0, temp.indexOf('/')).trim(); data[1][at] = temp.substring( temp.indexOf('/') +1 ).trim(); temp = fin.readLine(); at++; } fin.close(); } catch(IOException e) { System.err.println("Default.txt is not found."); } finally { return data; } } static int binSearch(String[] arr, int left, int right, String what) { if ( left > right ) { return -1; } //find middle int mid = (left + right) /2; int temp; //temp var to hold comparison result if((temp = what.compareToIgnoreCase(arr[mid])) == 0) { return mid; } else if ( temp < 0 ) { //string we're looking at is less than the one in the middle return binSearch(arr, left, mid - 1, what); } else if ( temp > 0 ) { //more than the one in the middle return binSearch(arr, mid+1, right, what); } return -1; } static void print(String[] s) { for (int i = 0; i < s.length; i++) { System.out.print(s[i] + " "); }System.out.println(); } }