【问题标题】:Issue with Scanner and While loop to check an arraylistScanner 和 While 循环检查数组列表的问题
【发布时间】:2015-04-24 06:52:46
【问题描述】:

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

以下代码旨在使用扫描仪输入作者和书名,以检查该书(由作者和书名的精确匹配组成)是否在数组列表中。

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

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;

【问题讨论】:

    标签: java arraylist java.util.scanner bluej


    【解决方案1】:

    事实证明,另一个答案让我思考(我今天还没有这样做)。 显然,我正走在我想要的正确轨道上,我只需要重新安排一些东西。虽然下面的代码有效,但另一种方法更容易阅读。

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

    【讨论】:

      【解决方案2】:

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

      如果您想阅读更多内容,请使用nextLine() 阅读整行。

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

      还有一些其他问题:

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

      您的代码应该看起来更像:

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

      【讨论】:

      • 好消息是,修复后,它可以正确显示线条,从而消除了我大脑中的混乱。但是,它在重复询问与数组列表的大小相对应的问题。当我在没有扫描仪的情况下使用它时,它只问了一次。此外,这些字段似乎没有保存,因为即使我正确给出了 4 次列出的书,它也说找不到该书。
      • 如果发现 book 停止迭代,你究竟在哪里设置 isFoundtrue
      • 现在我知道我不应该在哪里设置它了。
      • 不幸的是,在添加第二个 isFound 语句时我有点不知所措。
      • 我很感激,但不幸的是,我被告知要坚持使用 While 循环并以相同的方法使用扫描仪来输入作者和标题。
      猜你喜欢
      • 1970-01-01
      • 2021-05-10
      • 2011-06-19
      • 1970-01-01
      • 1970-01-01
      • 2012-03-13
      • 2018-04-22
      • 2020-10-28
      • 2011-03-04
      相关资源
      最近更新 更多