This is a library program with a .txt file with book names and reference numbers
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
Scanner keyboard = new Scanner(System.in);
String[] bookList = new String[100];
int[] bookNumber = new int[100];
int count = 0;
int search;
boolean found = false;
File file = new File("bookList.txt");
Scanner inputFile = new Scanner(file);
while (inputFile.hasNext()) {
bookNumber[count] = inputFile.nextInt();
bookList[count] = inputFile.nextLine();
count++;
}
inputFile.close();
System.out.print("Enter the book number to search for: ");
search = keyboard.nextInt();
for (int x = 0; x < count; ++x) {
if (bookNumber[x] == search) {
System.out.println("Book number " + search + " is " + bookList[x]);
found = true;
}
}
if (!found) {
System.out.println("Book number " + search + " was not found.");
}
System.out.println();
System.out.print("Enter the book number to search for: ");
search = keyboard.nextInt();
int low = 0;
int high = count - 1;
int middle;
while (low <= high) {
middle = (low + high) / 2;
if (bookNumber[middle] == search) {
System.out.println("Book number " + search + " is " + bookList[middle]);
found = true;
break;
} else if (bookNumber[middle] > search) {
high = middle - 1;
} else {
low = middle + 1;
}
}
if (!found) {
System.out.println("Book number " + search + " was not found.");
}
}
}```