使用Scanner和While循环来检查arraylist

时间:2015-02-23 01:02:16

标签: java arraylist java.util.scanner bluej

通常,我在扫描某些元素的数组列表时没有问题。我知道如何构建while循环等。但是,在这种情况下,我需要使用扫描仪,但它给出了我的问题,如下所示:

enter image description here

以下代码旨在使用扫描程序输入作者和标题,以检查该精确书籍(包括作者和标题的精确匹配)是否在数组列表中。

我很可能忽略了一些简单的事情,但无论如何,我不需要任何评论来评论这是一个愚蠢的代码等。

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;

2 个答案:

答案 0 :(得分:0)

next()只返回一个令牌(字),因此对于像The Prince这样的数据,首先next()会返回"The"next()将返回"Prince" (因此它不会等待来自用户的输入,因为它已经有了它的标记)。

如果您想阅读多个单词,请使用nextLine()读取整行。

如果您想在next()nextLine()代码中使用,请阅读Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods

还有其他一些问题:

  • 如果找到图书,则不会将isFound设置为true;
  • 每次迭代书时你都要求书名和作者,但你应该在迭代之前知道这些信息,所以也许让用户将这些信息作为方法参数传递。
  • 您正在将用户提供的值与来自""title字段的空字符串(author)进行比较

您的代码可能看起来更像:

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);

    }
}

答案 1 :(得分:0)

事实证明,其他答案让我思考(我今天还没有完成)。 显然我正在寻找我想要的东西,我只需要重新排列东西。虽然下面的代码有效,但另一种方法更容易阅读。

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;
}