3

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

3
  • No one can possibly understand what you're trying to achieve in the second snippet. But, are you asking How to access the people stored in the list arraylist? Commented Jan 7, 2016 at 11:53
  • Could create a Persons/People class which either IS A or HAS A List<Person> and then implement your own toString() method there? Is that what you mean? Commented Jan 7, 2016 at 11:55
  • Again I'm sorry for not being clear well what I'm doing is whatever I add in the list and I will fetch the object from the ArrayList using the toList wherein it will show both the age and name that I inputed Commented Jan 7, 2016 at 11:58

4 Answers 4

1

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.

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

3 Comments

Thank you it worked and thanks to all the people who helped me :D
one last question : How do you separate your input because it continues like a sentence and not just by line
Use \n new line separator or something which you need.
1

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()

Comments

1

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.

Comments

1
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);
    }

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.