0

I am asking user candidate info then i am calling 'addcandidate' method and i am trying to hold candidate info in array list.By the way if user want to add new candidate i am getting again their info and want to add my arraylist.But its holding only last candidate info

my addcandidate method code like this

static  void addcandidate(String name,String surname,String adress,String talent){
    List<List<Object>>listoflist=new ArrayList<>();
    List<Object>candidatelist=new ArrayList<Object>();
    candidatelist.add(name);
    candidatelist.add(surname);
    candidatelist.add(adress);
    candidatelist.add(talent);
    listoflist.add(candidatelist);
    

    System.out.println(listoflist);

}

and also mycode that to ask candidate info like that

  while (true){
        Scanner oku=new Scanner(System.in);
        System.out.println("what kind of process do you want ? 1-Add Candidate 2-Add Employee " +
                "3- add employee from candidate 4-search candidate for talent 5-print wiew");
        int processtype=oku.nextInt();

        if (processtype==1){
            System.out.println("please enter candidate name");
            String cname=oku.next();
            System.out.println("please enter candidate sname");
            String csname=oku.next();
            System.out.println("please enter candidate adress");
            String cadress=oku.next();
            System.out.println("please enter candidate talent");
            String ctalent=oku.next();
            Candidate.addcandidate(cname,csname,cadress,ctalent);
            System.out.println("do you want to continue");
            String answer=oku.next();
            if (answer=="n"){
                break;
            }


        }
    }

my output expectation is like this [[tom,jhn,usa,it],[peter,parker,england,language]]

4
  • listOflist is only stored in a local variable in addcandidate, and you create a new list each time you call addcandidate. Commented Aug 6, 2020 at 4:44
  • thank you for your answer but how can i create new list each time call addcandidate can you give example please Commented Aug 6, 2020 at 4:51
  • I mean that's what you are doing at the moment. You need to add the candidates to a list whose lifetime is the same as your entire program. Commented Aug 6, 2020 at 4:54
  • Use class to store per candidate info Commented Aug 6, 2020 at 4:57

2 Answers 2

2

You need declare listoflist out of method addcandidate. For example like this.

import java.util.ArrayList;
import java.util.List;

public class Main {

    public static List<List<Object>> listoflist = new ArrayList<>();

    public static void main(String[] args) {
        addcandidate("Name 1", "Surname 1", "Street 1", "Music");
        addcandidate("Name 2", "Surname 2", "Street 2", "Singing");
        addcandidate("Name 3", "Surname 3", "Street 3", "Sport");
    }

    public static void addcandidate(String name, String surname, String adress, String talent){
        List<Object> candidatelist = new ArrayList<Object>();
        candidatelist.add(name);
        candidatelist.add(surname);
        candidatelist.add(adress);
        candidatelist.add(talent);
        listoflist.add(candidatelist);


        System.out.println(listoflist);

    }
}

The result of executing this code.

[[Name 1, Surname 1, Street 1, Music]]
[[Name 1, Surname 1, Street 1, Music], [Name 2, Surname 2, Street 2, Singing]]
[[Name 1, Surname 1, Street 1, Music], [Name 2, Surname 2, Street 2, Singing], [Name 3, Surname 3, Street 3, Sport]]
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much very good it's working I appreciate
@BasitAnlat Welcome, I'm glad to hear that my answer was useful.
1

Your listoflist declaration is inside addCandiadte method so each time the array list is being created and finally you are having the list that was created last. Do something like below:

// outside addCandiadte method
 List<List<Object>>listoflist=new ArrayList<>();

static  void addcandidate(String name,String surname,String adress,String talent){
   
    List<Object>candidatelist = new ArrayList<Object>();    
    
    candidatelist.add(name);
    candidatelist.add(surname);
    candidatelist.add(adress);
    candidatelist.add(talent);
    
    listoflist.add(candidatelist);
    

    System.out.println(listoflist);

}

Hope you got the point. Additionally use List<String>candidatelist instead of List<Object>candidatelist for better readability and type checking.

2 Comments

Thank you so much I appreciate all of you
Hi @Basit Anlat, glad to help you. Please "Accept" one answer so that it will help other readers on SO. You can also vote multiple answers. stackoverflow.com/help/someone-answers

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.