Write and print data to an ArrayList

I am doing a college job and I need to save in an arrayList the name and age of 10 people and then print the data of the person who is in position 7, but when I print it returns null. I don't know if it's not saving the data in arrayList or if it's not being able to fetch for print.

Officer Class

package teste;

import java.util.ArrayList;
import java.util.Scanner;

public class Funcionario {

    private static Scanner s;

    public static void main(String[] args) {

        s = new Scanner(System.in);
        ArrayList<Pessoa> listaPessoa = new ArrayList<Pessoa>();    
        Pessoa pessoa = new Pessoa();

        for(int i=0; i < 10; i++){  

            System.out.println("\nDigite o nome:");
            pessoa.nome = s.next();
            System.out.println("\nDigite a idade:");
            pessoa.idade = s.nextInt();
            listaPessoa.add(new Pessoa());

        }
        System.out.println(listaPessoa.get(7));
    }

}

Person Class

package teste;

public class Pessoa {

    public String nome;
    public int idade;


    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome=nome;
    }

    public int getIdade() {
        return idade;
    }

    public void setIdade(int idade) {
        this.idade=idade;
    }

    public String toString(){
        return nome + " " + idade;
    }

}
Author: Gabriel Berlanda, 2017-06-07

2 answers

The problem is that you are storing an "empty" person object and filling in another object. Change to the following:

import java.util.ArrayList;
import java.util.Scanner;

public class Funcionario {

    private static Scanner s;

    public static void main(String[] args) {

        s = new Scanner(System.in);
        ArrayList<Pessoa> listaPessoa = new ArrayList<Pessoa>();    

        for(int i=0; i < 10; i++){  
            Pessoa pessoa = new Pessoa();
            System.out.println("\nDigite o nome:");
            pessoa.nome = s.next();
            System.out.println("\nDigite a idade:");
            pessoa.idade = s.nextInt();
            listaPessoa.add(pessoa);

        }
        System.out.println(listaPessoa.get(7));
    }

}

Now, with each iteration of the loop, a new Pessoa object will be created and added to the ArrayList after it is populated.


If you don't want Pessoa to be created empty(i.e., with no name and age), you can create a constructor and force these values to be reported right when you create the object.

And there is also a violation in the encapsulating the properties of your class, the purpose of the getters and setters is precisely to avoid this type of access, if you leave the members public, the getters and setters end up not serving their purpose.

With the suggested changes, your class Pessoa would look like this:

public class Pessoa {

    private String nome;
    private int idade;

    public Pessoa(String nome, int idade)
    {

        this.nome = nome;
        this.idade = idade;

    }


    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome=nome;
    }

    public int getIdade() {
        return idade;
    }

    public void setIdade(int idade) {
        this.idade=idade;
    }

    public String toString(){
        return nome + " " + idade;
    }

}

And its loop in class Funcionario happens to work like this:

for(int i=0; i < 10; i++){  
    System.out.println("\nDigite o nome:");
    String nome = s.next();
    System.out.println("\nDigite a idade:");
    int idade = s.nextInt();
    listaPessoa.add(new Pessoa(nome, idade));

}
 1
Author: , 2017-06-07 15:41:55

Isadora, you always get null, because when you add the person to the list, you always add a new person instance that is empty.

listaPessoa.add(new Pessoa());

The correct way to perform this method would be to instantiate a new person within the for, give the name and age sets, and add that instance within your list.

    for(int i=0; i < 10; i++){  
        Pessoa pessoaAdicionada = new Pessoa();
        System.out.println("\nDigite o nome:");
        pessoaAdicionada.nome = s.next();
        System.out.println("\nDigite a idade:");
        pessoaAdicionada.idade = s.nextInt();
        listaPessoa.add(pessoaAdicionada);
    }

Can remove the line you instance a new person before the "for".

Let's review what was done:

We instantiate the person within do for in this line:

Pessoa pessoaAdicionada = new Pessoa();

This instance of added person will be a new instance in each repetition of for, after instantiating the person to be added, We assign the values to it, taking from the scanner the name and age.

After we read the person's name and age values, we take the added Person object that will be with the filled in name and age values and add it within your person list.

 0
Author: Gabriel Berlanda, 2017-06-07 15:40:04