Display data from an array of objects stored in memory

Asked

Viewed 370 times

-1

I cannot make a method that returns the information of registered students.

package menu;

import java.util.Scanner;

import modelo.Aluno;
import repositorio.RepositorioAluno;

public class AlunoMain {

    static Scanner s = new Scanner(System.in);
    static RepositorioAluno repositorio = new RepositorioAluno();

    public static void main(String[] args) {

        menu();

    }

    public static void cadastrarAluno() {

        Aluno aluno = new Aluno();

        System.out.println("Nome do aluno: ");
        aluno.setNome(s.next());
        System.out.println("Cpf: ");
        aluno.setCpf(s.next());
        System.out.println("Email: ");
        aluno.setEmail(s.next());
        System.out.println("Instituição de ensino: ");
        aluno.setInstituicao(s.next());

        if (repositorio.cadastrarAluno(aluno) == true) {
            System.out.println("Aluno cadastrado!");
        } else {
            System.out.println("Falha no cadastro!");
        }

        System.out.println("9-Menu");
        int menu = s.nextInt();
        if (menu == 9) {
            menu();
        }

    }



    public static void menu() {
        boolean terminar = false;

        System.out.println(
                "1-Cadastrar aluno | 2- Remover Aluno | 3-Procurar Aluno | 4-Exibir alunos cadastrados | 0-Sair");
        int escolher = s.nextInt();

        do {

            switch (escolher) {
            case 1:
                cadastrarAluno();

            case 2:
                removerAluno();

            case 3:
                procurarAluno();

            case 4:
                exibirCadastrados();
            }

        } while (terminar);
    }
}

Here is the method that is in the class RepositorioAluno I’m trying to make it work

public Aluno exibirCadastrados() {
        Aluno[] alunosCadastrados = new Aluno[100];

        for (int i = 0; i < listaAluno.length; i++) {
            if (listaAluno[i] != null) {
                alunosCadastrados[i] = listaAluno[i];
                System.out.println(alunosCadastrados);
            }
        }
        return alunosCadastrados[];
    }

1 answer

0


This method claims to display the registrants, but it also copies, I don’t know why, the data to another array and returns it, I would take all this because it doesn’t make sense. But to display it just have the element display instead of having the array.

public Aluno[] exibirCadastrados() {
    Aluno[] alunosCadastrados = new Aluno[100];
    for (int i = 0; i < listaAluno.length; i++) {
        if (listaAluno[i] != null) {
            alunosCadastrados[i] = listaAluno[i];
            System.out.println(alunosCadastrados[i]);
        }
    }
    return alunosCadastrados;
}

I put in the Github for future reference.

There simplifies:

public void exibirCadastrados() { for (Aluno aluno : listaAluno) if (aluno != null) System.out.println(aluno); }

The code has several other problems, but is not the focus of the question.

  • I would be happy if you informed me of the various problems, so I would try to improve later. Thank you.

Browser other questions tagged

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