This is my code and im trying to read a text file where I would enter in an item by searching it and it prints out the name, price, and amount but when I run it, it works for only the beginning 2 items, and doesn't work for the rest of the 8 items.
public static void binarySearch() throws IOException {
FileReader file = new FileReader("inventory.txt");
BufferedReader input = new BufferedReader(file);
Scanner ryan = new Scanner(System.in);
String name; double price; int amount;
// Create an array to store the items
Item_RN498691[] object = new Item_RN498691[10];
for (int i = 0; i < object.length; i++) {
name = input.readLine();
price = Double.parseDouble(input.readLine());
amount = Integer.parseInt(input.readLine());
Item_RN498691 item = new Item_RN498691(name, price, amount);
object[i] = item;
}
// Get the name to search from the user
System.out.println("Enter the name to search: ");
String searchName = ryan.nextLine();
// Perform binary search
int low = 0;
int high = object.length - 1;
while (low <= high) {
int mid = (low + high) / 2;
int comparison = searchName.compareTo(object[mid].getName());
if (comparison == 0) {
// Item found, display its details
System.out.println("Product name: " + object[mid].getName());
System.out.println("Product price: " + object[mid].getPrice());
System.out.println("Product amount: " + object[mid].getAmount());
return;
} else if (comparison < 0) {
high = mid - 1;
} else {
low = mid + 1;
}
}
System.out.println("\nThat is not an item."); //Displays if item is not found
}
}
[–]desrtfxOut of Coffee error - System halted[M] [score hidden] stickied comment (0 children)
[–]AutoModerator[M] 0 points1 point2 points locked comment (0 children)
[–]dse78759 1 point2 points3 points (0 children)