2017-05-11 8 views
2

私のOdyseeのJavaの学習の一環として、クラスBookとクラスライブラリを実装する必要があるタスクがあります。 1つの図書館に最大10冊の図書を保管し、図書館の書籍を検索することもできます。今私の問題は、私の検索方法がうまくいかないことです。ここに誰かが考えていると思いますが、これは自分のコードです:基本的なJavaライブラリ、検索方法は機能しません

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

ありがとうございました!

+0

読み取りを。変数は小文字で始まるはずです – Jens

+0

'result = this.storage [i] .toString();'の行の後に戻るか、この行を 'this.storage [i] .toString();' – Jens

+0

なぜ新しい本を作るのですか?あなたは書籍の配列を検索しています - 一致するものを返します。 – duffymo

答えて

3

あなたが見つからなかった場合にのみ、それが見つかった書籍、およびnullを返す必要がありますJavaの命名規則について

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

私はこれを試してみました。ありがとうございました! – djaszak

4

あなたの問題はここにある:

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

ここでは、一致するものを見つけるためにあなたの本のすべてをループしようとしている(それが何のループがためであることはそれではないでしょうか?)。悲しいことに、これは実際にはnullの最初のという本に返します。

あなたはこのようなものを必要とする:

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

注ここで我々は(または私達はそれを見つけた)我々は、書籍のすべてを見てきました後の本を見つけたかどうかを確認する方法。

+0

私は休憩を追加する方が良いでしょう。見つかった後= this.storage [i];より効果的です(本が見つかるとフルスキャンを実行する必要はありません) – Nemesis

+0

ああ、問題を理解していただきありがとうございます。私はちょうどelseステートメントを削除しました。今、私はループを持って、それが動作し、それが一致する本を見つけることができません、それはちょうどループを終了し、メッセージを印刷し、nullを返します。助けてくれてありがとう! – djaszak

+0

パフォーマンスは私が信じる場合の目標ではありません。 – kharandziuk

関連する問題