#I have no idea what is going wrong here:

15 messages · Page 1 of 1 (latest)

bronze panther
#

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.");
    }
  }
}```
brisk prawnBOT
#

This post has been reserved for your question.

Hey @bronze panther! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

#

<@&765578700724371486>

Requested by Λesop#7818
bronze panther
#

Theres some sort of scanner issue

light hull
#

What does the text file look like?

#
while (inputFile.hasNext()) {
      bookNumber[count] = inputFile.nextInt();
      bookList[count] = inputFile.nextLine();
      count++;
    }
#

This snippet of code doesn't make sense

bronze panther
light hull
#
while (inputFile.hasNext()) {
  bookNumber[count] = inputFile.nextInt();
  inputFile.nextLine(); // Consume the newline character
  bookList[count] = inputFile.nextLine();
  count++;
}
#

try this

bronze panther
#

Okay let me try

#

THAT WORKED

#

THANK YOU

brisk prawnBOT
# bronze panther THANK YOU

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.