Write and print data in an ArrayList

1

I'm doing a college job and I need to store the name and age of 10 people in an array and then print the data of the person who is in position 7, but when I go to print, returns null. I do not know if you are not saving the data in the arrayList or if you are not able to fetch for printing.

Class Officer

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

}
    
asked by anonymous 07.06.2017 / 17:30

2 answers

1

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 object Pessoa will be created and added to the ArrayList after it is populated.

If you do not want Pessoa to be created empty (that is, no name and age), you can create a constructor and force these values to be immediately informed when the object is created.

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

With the suggested changes, your Pessoa class 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 your loop in the Funcionario class starts working this way:

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

}
    
07.06.2017 / 17:33
0

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

listaPessoa.add(new Pessoa());

The correct way to accomplish this method would be to instantiate a new person within the for, give the name and age sets and add that instance to 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);
    }

You can remove the line that you instantiate a new person before the "for".

Let's review what was done:

We instantiate the person inside the for in this line:

Pessoa pessoaAdicionada = new Pessoa();

This instance of an added person will be a new instance in every for loop, after instantiating the person to be added, we assign the values to it, taking the name and age from the scanner.

After reading the name and age values of the person, we get the personAdded object that will have the name and age values filled in and added to your person list.

    
07.06.2017 / 17:40