简体   繁体   English

基本Java库,搜索方法不起作用

[英]Basic Java Library, searching method does not work

As a part of my Odysee on learning Java, we got a Task in which we have to implement the class Book and the class Library. 作为我学习Java的Odysee的一部分,我们得到了一个Task,我们必须实现类Book和类库。 You should store up to 10 books in one library also you should have the possibility to search for books in the library. 您应该在一个图书馆中存储多达10本书,您也应该可以在图书馆中搜索图书。 Now my problem is that my search method doesn't work. 现在我的问题是我的搜索方法不起作用。 Maybe someone here has an idea, this is my code: 也许这里有人有个主意,这是我的代码:

public class Library {
    int Capacity = 10;
    int Volume = 0;
    Book[] storage = new Book[10];

    public Library() {
        System.out.println("Hello, I am a library, which can store up to 10 books!");
        this.storage = new Book[10];
    }

    public void add(Book book) {
        if (Volume < Capacity) {
            this.storage[Volume] = book;
            System.out.println("I added the book " + book + ".");
            Volume++;
        } else if (Volume >= Capacity) System.out.println("The library is full!");

    }

    public Book search(String title) {
        String result = new String();
        for (int i = 0; i < this.Volume; i++) {
            if (title.equals(this.storage[i].toString())) {
                System.out.println("The book with the title " + title + " exists in the library!");
                result = this.storage[i].toString();
            } else {
                System.out.println("The book with the title " + title + " does not exist in the library!");
                return null;
            }
        }
        Book retBook = new Book(result);
        return retBook;
    }
}


public class Book {
    String title;

    public Book(String title){
        this.title = title;
        System.out.println("Book " + title + " created.");}

    public String toString(){
        return this.title;
    };
}

Thanks for any help! 谢谢你的帮助!

Your problem is here: 你的问题在这里:

   for (int i = 0; i < this.Volume; i++) {
        ...
        } else {
            System.out.println("The book with the title " + title + " does not exist in the library!");
            // *** THIS LINE IS WRONG ***
            return null;
        }

Here you are trying to loop through all of your books to find the one that matches (that is what the loop is for isn't it?). 在这里,你试图遍历你的所有书籍以找到匹配的那个(这就是循环的用途不是吗?)。 Sadly, what this actually does is it returns null on the first book that doesn't match. 遗憾的是,它实际上做的是它在第一本不匹配的书上返回null

You need something like this: 你需要这样的东西:

public Book search(String title) {
    Book found = null;
    for (int i = 0; i < this.Volume && book == null; i++) {
        if (title.equals(this.storage[i].toString())) {
            System.out.println("The book with the title " + title + " exists in the library!");
            found = this.storage[i];
        }
    }
    // Check if we found it AFTER the loop completes.
    if (found == null) {
        System.out.println("The book with the title " + title + " does not exist in the library!");
    }
    return found;
}

Note here how we check to see if we've found the book after we've looked at all of the books (or we found it). 请注意我们在查看了所有书籍(或者我们找到了书籍) 之后如何检查我们是否找到了这本书。

You must return the book when it's found, and null only if none is found: 您必须在找到它时返回该书,并且只有在找不到该书时才返回:

public Book search(String title) {
    for (int i = 0; i < this.Volume; i++) {
        if (title.equals(this.storage[i].toString())) {
            System.out.println("The book with the title " + title + " exists in the library!");
            return this.storage[i];
        }
    }
    System.out.println("The book with the title " + title + " does not exist in the library!");
    return null;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM