blitzboy01
blitzboy01

Reputation: 53

store object in ArrayList using another class

This is the first code:

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,"Enter Name:");
                int age = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter Age:"));
                list.add(new Person(name,age)); 
                break;

            case 2:
                break;

            case 3:
                break;

            case 4:

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

            case 7:
                break;


            }

        }while(choice!=7);
    }

}

I'm sorry if I am not clear with my question but I want to use the ArrayList wherein I store the ArrayList in Person class and I want to view it using the public String toString so it will show the name and age

Upvotes: 3

Views: 556

Answers (4)

YoungHobbit
YoungHobbit

Reputation: 13402

Fetch the Person object from the list using list.get(index) method, then concatenate.

I guess, once you get all the person information, then you want to show. So take out the JOptionPane.showMessageDialog(null,listing); from for loop. Otherwise keep it inside the loop, as you have currently.

for(int i=0;i<list.size();i++){
     listing += list.get(i).toString() + "\n";  // get the i Person instance from list and call toString()     
}
JOptionPane.showMessageDialog(null,listing);

You are doing the String concatenation using + operator. You can also use the StringBuilder class to append all the Person information from the list.

Upvotes: 1

kism3t
kism3t

Reputation: 1361

public static void main(String[] args) {

        ArrayList<Person> list= new ArrayList<>();
        int choice =0;

        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,"Enter Name:");
                int age = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter Age:"));
                list.add(new Person(name,age)); 
                break;

            case 2:
                break;

            case 3:
                break;

            case 4:

                break;
            case 5:
                if(!list.isEmpty()){
                    foreach(Person p : list){
                        StringBuilder message = new StringBuilder();
                        message.append(p.toString());
                        message.append("\n"); 
                        JOptionPane.showMessageDialog(null,message.toString());
                    }
                }else{
                    JOptionPane.showMessageDialog(null,"ERROR","", JOptionPane.WARNING_MESSAGE);
                }
                break;
            case 6:
                break;

            case 7:
                break;


            }

        }while(choice!=7);
    }

Upvotes: 1

Leonardo Kenji Shikida
Leonardo Kenji Shikida

Reputation: 761

Your code seems to be inserting the Person class correctly inside the ArrayList. So it seems that, if you need to see the contents of your ArrayList using toString(), all you need is to override the method toString() in your Person class. So when you print your ArrayList, it will just call the toString() method for each ArrayList element.

Upvotes: 1

Mike Murphy
Mike Murphy

Reputation: 1046

Looks like the issue is with your case 5: code.

Try

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

Note the list.get(i).toString() instead of list.toString()

Upvotes: 1

Related Questions