代码之家  ›  专栏  ›  技术社区  ›  Jaquarh

在使用Java流和扫描仪时,我遇到了意想不到的行为

  •  4
  • Jaquarh  · 技术社区  · 6 年前

    我最近看到了一个关于大学课程的话题,这个话题是由一位朋友指导的,他被要求以某种方式完成。我想我会抓住这个机会去干这项工作。

    我创建了一个Book类,如下所示:

    class Book
    {
        private String author, title;
    
        public Book setAuthor(String a)
        {
            author = a;
            return this;
        }
    
        public Book setTitle(String t)
        {
            title = t;
            return this;
        }
    
        public String getAuthor() 
        {
            return author;
        }
    
        public String getTitle()
        {
            return title;
        }
    }
    

    private final static int BOOK_NO = 3;
    private final static SO instance = new SO(); // This is whatever you called the class
    
    public static void main(String[] args)
    {
        Book[] books = new Book[BOOK_NO];
        Scanner kybd = new Scanner(System.in);
    
        for(int i = 0; i < BOOK_NO; i++)
        {
            books[i] = instance.addBook(kybd, new Book());
        }
    
        Arrays.stream(instance.findBook(kybd, books)).forEach(o -> {
            System.out.println(o.getTitle() + " by " + o.getAuthor());
        });
    }
    
    public Book addBook(Scanner s, Book b)
    {
    
        System.out.println("Enter the Author of this book:");
        b.setAuthor(s.next());
    
        System.out.println("Enter the Title of this book:");
        b.setTitle(s.next());
    
        return b;
    }
    
    public Book[] findBook(Scanner s, Book[] bs)
    {
        System.out.println("Search a book by author:");
    
        List<Book> finding = Arrays .stream(bs)
                .filter(o -> o.getAuthor().equalsIgnoreCase(s.next()))
                .collect(Collectors.toList());
    
        System.out.println("Found " + finding.size() + " matches.");
    
        Book[] output = new Book[finding.size()];
        output = finding.toArray(output);
        return output;
    }
    

    现在,整个程序运行良好,但当涉及到搜索书籍时,我在扫描仪上遇到了意想不到的行为。以下是我正在经历的直接输入/输出行为:

    Enter the Author of this book:
    Foo
    Enter the Title of this book:
    Bar
    Enter the Author of this book:
    Foo
    Enter the Title of this book:
    FooBar
    Enter the Author of this book:
    Bar
    Enter the Title of this book:
    Foo
    Search a book by author:
    Foo
    Foo
    Foo
    Found 2 matches.
    Bar by Foo
    FooBar by Foo
    

    正如你所看到的,在得到任何结果之前,我必须在扫描仪中输入这本书的作者3次。我如何减轻这种影响?是什么导致了这种情况?

    1 回复  |  直到 6 年前
        1
  •  3
  •   GBlodgett    6 年前

    这是因为在你的 Stream 你打电话 next() ,所以每一个 Book 中的对象 流动 这个 Predicate 在中,对筛选器的调用应用于它,并且 下一个()

    String book = s.next();
    List<Book> finding = Arrays.stream(bs)
                               .filter(o -> o.getAuthor().equalsIgnoreCase(book))
                               .collect(Collectors.toList());
    

    filter() 接受 Predicate ,在这种情况下,将类似于:

    Predicate<String> pred = str -> str.equalsIgnoreCase(s.next());
    

    所以每次应用的时候,, 下一个() 将被称为

    推荐文章