Write and print data in an Arraylist

Asked

Viewed 1,709 times

1

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 at position 7, but when I print returns null. I do not know if you are not storing the data in the arrayList or if you are not able to search for printing.

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

}

Classe Pessoa

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

}

2 answers

1


The problem is that you are storing an "empty" person object and filling 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 loop iteration, a new object will be created Pessoa and will be added to Arraylist after it is filled.


If it is not desired that Pessoa is created empty (ie without name and age), you can create a constructor and force these values to be informed right at the creation of the object.

And there is also a violation in the encapsulation of the properties of your class, the purpose of getters and setters is precisely to avoid this type of access, if you leave public members, getters and setters end up not serving your 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 your tie in the class Funcionario will work that 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));

}
  • 1

    I’m learning java now and I’m picking up a little... Thank you so much for the simple and objective answer. Diego. =)

  • @Isadoraoliv :)

0

Isadora, you always get null, because when adding the person in 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, 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);
    }

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

Let’s review what’s been done:

We instantiate the person inside the for this line:

Pessoa pessoaAdicionada = new Pessoa();

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

After reading the values of the person’s name and age, we take the personal object that will be with the values of name and age filled and add inside your person list.

  • Thanks for your help Gabriel =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.