1

Normally, I have no issues scanning an array list for certain elements. I am aware of how to structure the while loop etc. However, in this case, I need to use a scanner, but it is giving my issues as seen here:

enter image description here

The following code is intended to use a scanner to input an author and title to check if that exact book (consisting on a precise match of both author AND title) is in the array list.

Most likely I am overlooking something simple, but in any event, I don't need any comments commenting on this being a dumb code etc.

public String checkForBookUsingInfo(){
    int index = 0;
    Book bookObject = null;
    String returnValue = "Book not found";
    String title = "";
    String author = "";
    Boolean isFound = false;
    while (index <bookList.size() && isFound == false ){
        bookObject = bookList.get(index);
        System.out.println("Please enter title of book to search for.");
        String anyTitle = keybd.next();
        System.out.println("Please enter author of book to search for.");
        String anyAuthor = keybd.next();
        if ((title.equals(anyTitle)) && (author.equals(anyAuthor))){
            returnValue = "Book is in library.";
        }
        index++;
    }
    return returnValue;
Pshemo
  • 113,402
  • 22
  • 170
  • 242
Frog666
  • 15
  • 1
  • 3

2 Answers2

0

next() returns only one token (word), so for data like The Prince first next() will return "The" second next() will return "Prince" (so it will not wait for input from user since it already has its token).

If you want to read more then one word read entire line with nextLine().

In case you would want to use in your code both next() and nextLine() you should read Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods

There are few other problems:

  • you are not setting isFound to true if book will be found;
  • you are asking for book title and author every time while you iterate over books, but you should know this informations before iterations, so maybe let user pass this informations as methods arguments.
  • you are comparing values provided by user with empty Strings ("") from title and author fields

Your code should probably look more like:

class Book{
    private String author;
    private String title;

    //getters and setters
}


class Library {

    private List<Book> bookList = new ArrayList<Book>();

    public String checkForBookUsingInfo(String author, String title){
        for (Book book : bookList){
            if (book.getAuthor().equals(author) && book.getTitle().equals(title)){
                return "Book is in library.";
            }
        }
        return "Book not found in library";
    }

    public static void main(String[] args) throws Exception {
        Scanner keybd = new Scanner(System.in);
        Library library  = new Library();
        //add some books to library
        //....

        System.out.println("Please enter title of book to search for.");
        String anyTitle = keybd.nextLine();

        System.out.println("Please enter author of book to search for.");
        String anyAuthor = keybd.nextLine();

        String stateOfBook = library.checkForBookUsingInfo(anyAuthor, anyTitle);
        System.out.println(stateOfBook);

    }
}
Community
  • 1
  • 1
Pshemo
  • 113,402
  • 22
  • 170
  • 242
  • Well the good news is that after fixing that, it displays the lines correctly, thus eliminating the confusion in my brain. However, it is repeating itself in asking the questions to correspond with the size of the array list. When I had it going without the scanner, it only asked once. Also, the fields don't seem to be saving since even when I give a listed book correctly 4 times, it says the book was not found. – Frog666 Feb 23 '15 at 01:15
  • And where exactly are you setting `isFound` to `true` if book was found to stop iterations? – Pshemo Feb 23 '15 at 01:16
  • Well I know now where I shouldn't set it. – Frog666 Feb 23 '15 at 01:18
  • Unfortunately I'm a bit clueless when it comes to adding the second isFound statement. – Frog666 Feb 23 '15 at 01:24
  • I appreciate it, but unfortunately I was told to stick with a While loop and use a Scanner in the same method to input author and title. – Frog666 Feb 23 '15 at 01:44
  • `for(Type t : elements){stuff;}` is essentially `for (int i = 0; i – Pshemo Feb 23 '15 at 01:50
0

Turns out, that other answer got me thinking (which I have not done yet today). Apparently I was on the right track for what I wanted, I just needed to rearrange stuff. Although the below code works, the other method is by far easier to read.

public String checkForBookUsingInfo(){
    int index = 0;
    Book bookObject = null;
    String returnValue = "Book not found";
    String title = "";
    String author = "";
    Boolean isFound = false;
    System.out.println("Please enter the name of a book to search for.");
    String anyTitle = keybd.nextLine();
    System.out.println("Please enter the name of an author to search for.");
    String anyAuthor = keybd.nextLine();
    while (index <bookList.size() && isFound == false ){
        bookObject = bookList.get(index);
        title = bookObject.getTitle();
        author = bookObject.getAuthor();
        if ((title.equals(anyTitle)) && (author.equals(anyAuthor))){
            returnValue = "Book is in library.";
        }
        index++;
    }
    return returnValue;
}
Frog666
  • 15
  • 1
  • 3