Domanda

In advance, please forgive me if I do not give adequate background information for my question. Long time reader, first time asker.

I am making a program where one has a database of cars accessed through a tab delimited .txt file (we did something like this recently in my programming class, so I wanted to expand upon it).

Instead of using the terminal window, my format is displaying the Car objects (containing make, model, year, price, etc.) in ArrayList. I'm using JFrame, a JList, and a ListModel since I'm using an array of Car objects.

In my program, I wanted to create a delete method where the user could delete items from the database. Initially they would select the item from the JList and then would click on the delete button. This invokes the delete() method, which is the tab shown below...

void delete()
    {
        int i = list.getSelectedIndex();
        String string = (String)listModel.getElementAt(i); 


        for(Car c : cars)
        {
            String year = String.valueOf(c.getYear());
            String conditionReport = String.valueOf(c.getConditionReport());
            String price = String.valueOf(c.getPrice());

            if(c.getMake().indexOf(string) != -1 && c.getModel().indexOf(string) != -1 && year.indexOf(string) != -1 && conditionReport.indexOf(string) != -1 && price.indexOf(string) != -1 && c.getWarranty().indexOf(string) != -1 && c.getComments().indexOf(string) != -1)
            {
                int choice = JOptionPane.showConfirmDialog(null, "Are you sure you would like to remove the "  + cars.get(i).getYear() + " " + cars.get(i).getMake() + " " + cars.get(i).getModel() + "?", "Choose One", JOptionPane.YES_NO_OPTION);
                if(choice == JOptionPane.NO_OPTION || choice == JOptionPane.CLOSED_OPTION)
                {
                    return;
                } else
                {
                    cars.remove(c);
                    listModel.removeElementAt(i);
                }
            }
        } 


        writeFile();
    }

I have pinpointed my issue to be inside the if statement. (I printed things before and after to try to find where the program is lying. 'list' is my JList and 'listmodel' is my default list model. Car is an object I created that contains the elements (as seen by the get methods). The elements shown in the listModel are merely Strings that show getMake(), getModel(), and so forth... (Each 'get' item is separated by about 10 spaces.)

What am I doing wrong in the if statement? I figured that the getMake() and getModel() (and so forth) would be substrings of the index selected.

Thank you so much for your assistance! Any input regarding ways I could make further questions more specific and clear would be greatly appreciated!

È stato utile?

Soluzione

It seems like you are doing this to find the selected Car in some kind of data structure. You would be better off doing something like programming a custom list model that had access to cars itself. Then you could retrieve the selection more immediately. If cars is an ArrayList that list merely parallels I also don't see why you can't do something to the effect of cars.remove(list.getSelectedIndex());. Or since JList can display any object, override Car's toString to display what the list currently displays and have the list display Cars. Then you can cars.remove((Car)list.getSelectedValue());.

But aside from that, based on your description it sounds like you mean to do the evaluation the other way. It's the list item that should contain all of the Car attributes, rather than all of the Car attributes containing the list item. So something like

if( string.contains(c.getMake()) && string.contains(year) // and so on

(Or with indexOf but since contains merely returns indexOf > -1, using contains makes your code somewhat shorter.)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top