Help with Java codemount from ArrayList (search, insert String)

Asked

Viewed 966 times

2

Good evening, I have to develop a code, but I’m having some doubts, follow the code below:

public class Aluno {
String nome;
int mat;
Aluno(String nome, int mat) {
    this.nome = nome;
    this.mat = mat;
}

@Override
public String toString() {
    return nome + " - " + mat;
}

public static void main(String[] args) {
    int escolha;
    Scanner input = new Scanner(System.in);
    System.out.println("Digite o tamanho do array: ");
    int tamanho = input.nextInt();
    ArrayList<Aluno> alunos = new ArrayList<>(tamanho);

    Scanner choice = new Scanner(System.in);

    do {
        System.out.println(" Menu de opcões\n"
                + "1: Adicionar um nome na lista\n"
                + "2: adicionar um aluno em determinada posição do vetor\n"
                + "3: imprimir na tela o aluno em determinada posição do vetor\n"
                + "4: apagar (remover um aluno) de determinada posição do vetor\n"
                + "5: verificar de determinado aluno e/ou matrícula existem no vetor. Em caso positivo informar a posição do mesmo no vetor\n"
                + "6: imprimir a quantidade de alunos no vetor (não é o tamanho do vetor)\n"
                + "7: imprimir na tela todos os alunos\n"
                + "8:Sair");
        int opcoes = choice.nextInt();
        escolha = opcoes;
        switch (escolha) {

            case 1:
                System.out.println("Digite o nome do Aluno ");
                String nome = choice.next();
                System.out.println("digite o numero de matricula");
                int mat = choice.nextInt();
                Aluno novoAluno = new Aluno(nome, mat);
                alunos.add(novoAluno);
                break;

            case 2:
                System.out.println("digite o nome do Aluno!!");
                nome = choice.next();
                System.out.println("digite a  matricula");
                mat = choice.nextInt();
                System.out.println("digite a posiçao que o aluno vai estar!!");
                int c = choice.nextInt();
                novoAluno = new Aluno(nome, mat);
                alunos.add(c, novoAluno);
                break;

            case 3:
                System.out.println("Informe o numero da posiçao?");
                int n = choice.nextInt();
                if (n < tamanho) {
                    System.out.println("na posicao " + n + " esta: " + alunos.set(n, null));
                } else {
                    System.out.println("Fora dos limites");
                }
                break;
            case 4:

                System.out.println(alunos);

                System.out.println("qual posicao quer remover? ");
                int r = choice.nextInt();
                alunos.remove(r);
                break;

            case 5:
                System.out.println("qual aluno quer pesquisar");
                Scanner teste = new Scanner(System.in);
                String p = teste.nextLine();
                for (Aluno slt : alunos ){
                if (alunos.contains(p)) {
                    System.out.println("o aluno " + p + " esta na posiçao " + alunos.indexOf(p));
                } else {
                    System.out.println("Esse aluno nao existe!!");
                }
                }
                break;
            case 6:
                System.out.println("Existem " + alunos.size() + " Alunos no vetor");
                break;
            case 7:
                System.out.println("Os alunos sao: ");

                for (int i = 0; i < alunos.size(); i++) {
                    System.out.println(alunos.get(i));

                }
                break;
            default:
                System.out.println("Obrigado por utilizar o programa");

        }
    } while (escolha <= 7);
}


}

My doubts are as follows:

in Case 1, I’m not able to enter names with space, I tried to put nextLine, but it doesn’t work, if I put nextLine, in the execution of case1, only appears to type once the name and enrollment, all in the same line.

in Case 5, where I need to look for the student, I am not able to make him return me if the student is in the array and his position in the array, I tried both in the form above and the form below:

System.out.println("qual aluno quer pesquisar");
            String p = choice.next();
            if (alunos.contains(p)) {
                System.out.println("o aluno " + p + " esta na posiçao " + alunos.indexOf(p));
            } else {
                System.out.println("Nome nao esta Inserido!!");
            }

And the two don’t work.

All other cases work without any problem.

I appreciate the help.

1 answer

2


Case 1:

The Scanner leaves a line break in the input after the method call next(), you can discard it by calling the method nextLine().

System.out.println("Digite o nome do Aluno ");
choice.nextLine(); // descarta quebra
String nome = choice.nextLine();
System.out.println("digite o numero de matricula");
int mat = choice.nextInt();

Case 5:

Are you trying to see if a List of Alunos (where the methods equals and hashCode were not overwritten by the way) contains a String, this does not work. If you want to do a linear search then you will need to compare name with name:

for (int i = 0; i < alunos.size(); i++) {
    Aluno aluno = alunos.get(i);
    if (p.equals(aluno.nome)) {
        System.out.println("o aluno " + p + " esta na posiçao " + i);
        break;
    } else {
        System.out.println("Esse aluno nao existe!!");
    }
}
  • Whoa, thanks for the answer! , referring to case 5, it worked without any problems, now referring to case 1, if I leave the break as you reported, it adds an empty name inside the array, if I run case 7 to print the names, it returns me "empty" - "number plate". I’ll try other methods here, thanks for the help!

  • 1

    Crazy, you’re right, I switched the balls of case 1, in fact are the methods next* that leave the line break in the buffer. You must discard the line break before consuming the input of the new line.

  • Really now it worked, thanks!

Browser other questions tagged

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