alexandre kodok
alexandre kodok

Reputation: 1

Searching object

I want to search object inside arraylist get value from user input and print it to text area. here is the code.

//the arrayList I declared
  Book[]myBook = new Book [30];
  int index = 0;

private void searchBtnActionPerformed(java.awt.event.ActionEvent evt) {                                          

    String title = titleTF.getText();
    boolean found = false;
    for (int i = 0; i < index; i++) {
        if (myBook[i].getTitle().equals(title));
        {
            outputTA.append("Book Title : " + myBook[i].getTitle() + "\n");
            outputTA.append("Book Author : " + myBook[i].getAuthor() + "\n");
            outputTA.append("Year of Publication : " + myBook[i].getYear() + "\n");
            outputTA.append("Book Status : " + myBook[i].getStatus() + "\n");
            outputTA.append("======================================\n");
            found = true;
            break;
        }
    }

    if (found == false) {
        JOptionPane.showMessageDialog(this, "Book is not Found! Please Try again!");
    }
}

The problem is, when I click the search button, it will display the first object in the arraylist. Which line of code is wrong?

Upvotes: 0

Views: 27

Answers (1)

Alexandru Severin
Alexandru Severin

Reputation: 6248

First off, your index is 0 so your for doesn't loop. Replace index with myBook.size()

Upvotes: 1

Related Questions