1

This is the first code which is the Person Class:

public class Person {
    private int age;
    private String name;

    public Person(String name,int age){
        this.age = age;
        this.name = name;
    }

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

    public void setAge(int age){
        this.age = age;
    }

    public int getAge(){
        return age;
    }

    public String getName(){
        return name;
    }

    public String toString(){
        return name+","+age;
    }

}

Then this is the main class wherein it will show the output:

import java.util.ArrayList;

import javax.swing.JOptionPane;

public class PersonDatabase {

    public static void main(String[] args) {

        ArrayList<Person> list= new ArrayList<>();
        Person p = new Person("",0);
        int choice =0;
        String listing ="";

        do{
            choice=Integer.parseInt(JOptionPane.showInputDialog(null,"\nChoices:"+"\n[1]Add"+"\n[2]Edit"+"\n[3]Delete"+"\n[4]Search"+"\n[5]View"+"\n[6]Sort"+"\n[7]Exit"+"\nEnter Choice:"));


            switch(choice){
            case 1:
                String name = JOptionPane.showInputDialog(null,"\nEnter Name:");
                int age = Integer.parseInt(JOptionPane.showInputDialog(null,"\nEnter Age:"));
                list.add(new Person(name,age)); 
                break;

            case 2:
                try{
                    int index = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter index value:"));
                    name = JOptionPane.showInputDialog(null,"Enter Name:");
                    age = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter Age:"));
                    list.set(index,name,age);

                }catch(IndexOutOfBoundsException e){
                    JOptionPane.showMessageDialog(null,"Invalid Index");
                }

                break;

            case 3:
                break;

            case 4:

                break;
            case 5:
                if(!list.isEmpty()){
                    for(int i=0;i<list.size();i++){
                        listing+=list.get(i).toString()+"\n";
                    }
                    JOptionPane.showMessageDialog(null,listing);
                }else{
                    JOptionPane.showMessageDialog(null,"ERROR","", JOptionPane.WARNING_MESSAGE);
                }
                break;
            case 6:
                break;

            case 7:
                break;


            }

        }while(choice!=7);
    }

}

how do you use index so that you can search the object you inputted and then edit it using list.set()

1
  • you should not add the code from answers to the OP. It will confuse the future readers of the post. Commented Jan 7, 2016 at 13:42

3 Answers 3

3

If you know the index i of the Person object, list.get(i) will get you the object which you can modify in place. Since your Person object is mutable, you can do this:

Person p = list.get(i);
p.setName(name);
p.setAge(age);

This works because p refers to the same Person object as stored inside the list.

Sign up to request clarification or add additional context in comments.

Comments

3

Create the Person instance and then set it to the index.

list.set(index,name,age);

instead use

list.set(index,new Person(name,age));

You are already doing the same in add() method, while adding the Persons to the list. It is same, just use the set() method.


The issue is that, you are not resetting the listing each time view is called. In the start of the view you can reset it to the empty string. Add the below lines.

case 5:
   listing = "";

9 Comments

when I try to get the output it only adds but not edit the object
did you update the code provide in the answer above. It should do. list.set(index, element) -> Replaces the element at the specified position in this list with the specified element.
Make the changes in your code locally and run it. It should work.
when I only have one object inputted I can edit it but when I add another object then edit it it will only make a new object but not edit the index value I specified
put some sout and debug it. like what is the value of index or size of list currently.
|
0

You can instead edit by searching the username. By doing that, the application can guarantee that there will be no IndexOutOfBoundException. How? In case 2, first get the entered username. Then loop through the list to find him. Once found, use list.get(i) to get the user, edit it, and break the loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.