Hi, I am taking my first software design course, and I must integrate a menu and a binsearch in my current design. The whole program runs except for the search. Whenever the user enters a name to search, the program just spits back what they typed and returns to the menu as though it isn't searching at all. Any suggestions would be greatly appreciated.
Here is my print function which uses the search:
//this function prints an individual students report public static void PrintOne(int count, String name[], double average[], String grade[]) { int I; //row loop control variable String key; //initalize the searched name System.out.println(); System.out.println("Enter the student's name:"); System.out.println("Last, First"); Scanner keyboard = new Scanner(System.in); key = keyboard.next(); I = BinSearch(name,key); if(I == -1) System.out.printf(key, " could not be found.","\n"); else { System.out.println(" Parish School"); System.out.println(" Individual Student Report"); System.out.println("---------------------------"); System.out.println("Name Average Grade"); System.out.println("---------------------------"); System.out.printf("%17s", name[I]); System.out.printf("%8.2f", average[I]); System.out.printf("%7.2s\n", grade[I]); } }
and the actual search function:
//this function searches for a user-entered name public static int BinSearch(String name[], String key) { int begin, end, middle, position; Boolean found = false; begin = 0; end = 14; position = -1; while(begin <= end && !found) { middle = (begin + end) / 2; if(name[middle].toLowerCase().equals(key.toLowerCase())) { found = true; position = middle; return middle; } else if(name[middle].toLowerCase().compareTo(key.toLowerCase()) > 0) end = middle - 1; else begin = middle + 1; } return -1; }
Once again, I appreciate any and all suggestions, as my logic seems sound to me!