Hi,
I'm working on my final project and I'm almost done , but I notice error in my program
I have JList and csv file the user load the file then the user can add new item to the JList they need to enter the id,title,publisher ..etc
the problem is when I check for the id if it in the array or not it add the item and don't check if the id is already excist

here is my code for the add button

  try {
            int id = Integer.parseInt(txtID.getText());
            String author = txtAuthor.getText();
            String publisher = txtPub.getText();
            String title = txtTitle.getText();

            String pubDate = txtPubDate.getText();
            String category = txtCategory.getText();
            String subCat = txtSubCat.getText();
            double price = Double.parseDouble(txtPrice.getText());
            String issueNo = txtIssueNo.getText();
            String brandName = txtBrandName.getText();
            String desc = txtDesc.getText();
            String stock = txtStock.getText();
            int currId = 0;
            for (BookStore temp : bookStore) {
                currId = temp.getId();


            }
            if (currId == id) {
                JOptionPane.showMessageDialog(this, "Product with the " + id + " is already excist");
            } else {

                if (category.equals("Book")) {

                    Book b = new Book(title, author, publisher, pubDate, subCat, category, price, id, stock);
                     model.addElement(b);
                    bookStore.add(b);
                } else if (category.equals("Magazine")) {

                    Magazien m = new Magazien(category, title, issueNo, publisher, pubDate, subCat, price, id, stock);

                    model.addElement(m);
                    bookStore.add(m);
                } else if (category.equals("Office Supplies")) {

                    officeSupplies office = new officeSupplies(category, subCat, brandName, desc, price, id, stock);

                    model.addElement(office);
                     bookStore.add(office);

                } else {
                    JOptionPane.showMessageDialog(this, "The category " + category + " cannot be added");


                }

            }



        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, "Please select category then enter the data. you need to enter number for price and id");
            txtOutputErrors.append("Please select category then enter the data. you need to enter number for price and id");

        }

when the user enter the id number it should check if the id number is in the arrayList bookStore or not if it in the array then it should show window with error message if the id is not in the arrayList then it should add the object to the arrayList and the model JList

I've got the feeling that this is your problem:

for (BookStore temp : bookStore) {
                currId = temp.getId();
            }

you close your forloop there, so the id you are comparing to will always be the id of the very last element.

then again: your logic wouldn't fit if you addd the logic in there, neither.

try something like this:

boolean idFound = false;
for ( BookStore temp : bookStore ){
  if ( temp.getId() == id ){
    idFound = true;
    break;
  }
}

if ( idFound ){

// the element existsx already
}

else{}

I tried the code and now it work if the id is already excist then it show message , but when it not excist it doesn't add it to the JList and the arrayList

sorry I forgot to add line 3 now it work :D
thank you for your help ^^

Edited 3 Years Ago by sk8ergirl

This article has been dead for over six months. Start a new discussion instead.