kerinr
kerinr

Reputation: 85

Modifying an item in an arraylist coding issue

I am trying to change values of items in my arraylist. I can't seem to get this working. I am at a loss as to how to really ask this question. The code is quite extensive (or at least it is in my book) so I can't really show all of it. However if I know the current index, how can I make it change the ItemName?

 currentIndex.setItemName(newItemName);

CurrentIndex is an int that tells me which index I am at, ItemName is a string that is in my arraylist. Should I be getting the ItemName prior to trying to set it? Something like this

InventoryItem.getItemName();
currentIndex.setItemName(newItemName);

This also does not work.

Edit: I was asked to show more code. Here is the panel that pops up in my action listener

 modifyButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            JTextField xField = new JTextField(15);
            JTextField yField = new JTextField(15);
            JTextField zField = new JTextField(15);

            JPanel myPanel = new JPanel();
            myPanel.add(new JLabel("Item Name:"));
            myPanel.add(xField);

            myPanel.add(Box.createVerticalStrut(15)); // a spacer
            myPanel.add(new JLabel("Number in inventory:"));
            myPanel.add(yField);

            myPanel.add(Box.createVerticalStrut(15)); // a spacer
            myPanel.add(new JLabel("Unit Price:"));
            myPanel.add(zField);
            int result = JOptionPane.showConfirmDialog(null, myPanel,
                    "Please Enter data into all boxes", JOptionPane.OK_CANCEL_OPTION);
            if (result == JOptionPane.OK_OPTION) {

                String newItemName = String.valueOf(xField);
                String text1 = yField.getText();
                String newInventoryAmount = String.valueOf(text1);
                int newAmount = Integer.parseInt(newInventoryAmount);
                String text2 = zField.getText();
                String newUnitPrice = String.valueOf(text2);
                double newPrice = Double.parseDouble(newUnitPrice);
                inventory.get(currentIndex).setItemName(newItemName);
                inventory.get(currentIndex).setInStock(newAmount);
                inventory.get(currentIndex).setUnitPrice(newPrice);


            }
        }

}

);

Upvotes: 0

Views: 75

Answers (4)

kviiri
kviiri

Reputation: 3302

ìnt doesn't have a method setItemName (or any method at all, since it's a primitive, not an object).

Try yourArrayList.get(currentIndex).setItemName(newItemName);

It calls setItemName on the desired element of the list.

EDIT: to fix your new problem, replace String newItemName = String.valueOf(xField); with

String newItemName = xField.getText();

I believe this is what you want to do.

Upvotes: 1

Howcan
Howcan

Reputation: 313

I'm not sure what your ArrayList is name so I'll just call it arrayList.

Try

arrayList.get(currentIndex).setItemName(newItemName);

arrayList.get(currentIndex) calls the element from your list at the current index

That allows you to use .setItemName(newItemName) to change the name of the object.

Upvotes: 2

code4jhon
code4jhon

Reputation: 6044

Check out this code that accomplishes what I think you are trying to do.

public class Test {

    public static void main(String args[]){

        ArrayList<Employee> myArrayList = new ArrayList<Employee>();

        Employee e1 = new Employee();
        e1.setName("Juan");

        myArrayList.add(e1);

        myArrayList.get(0).setName("Jhon");

        System.out.println(myArrayList.get(0).getName());


    }

}

class Employee {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }



}

Upvotes: 0

Tim B
Tim B

Reputation: 41188

list.set(index, newItemName)

http://docs.oracle.com/javase/7/docs/api/java/util/List.html#set(int, E)

Upvotes: 1

Related Questions