Searching and comparing java

Asked

Viewed 40 times

2

I am preparing a program that must request the RA of a student, the RA must be greater than zero. Soon after I want to buy the second RA typed to verify that it has not already been registered. I wrote the code below but it keeps letting me register repeated numbers.

  if (nroAlunos == ra.length) {
       JOptionPane.showMessageDialog(null, "Não há mais espaço para cadastro.");} 
 for (int i = 0; i < ra.length; i++) {
    int cadastroRa = Integer.valueOf(JOptionPane.showInputDialog("Digite o RA que deseja cadastrar:"));
    if (cadastroRa == 0) {
        JOptionPane.showMessageDialog(null, "O número deve ser maior que zero...");
    } else {                            
            if (cadastroRa == ra[i]) {
                JOptionPane.showMessageDialog(null, "O RA digitado já esta cadastrado.");
            } else {
                ra[i] = cadastroRa;
                JOptionPane.showMessageDialog(null, "RA " + cadastroRa + " cadastrado com sucesso.");
            }
        }
    }
  • There are some problems in your approach: what if the vector is already full? Will it save over an existing ra? Another thing, every iteration that if it is false, it will save ra.

  • Above the for I have an if for validation if the array is fully filled.

  • 1

    So add to the question. I’m trying to elaborate a suggestion, but with parts missing gets complicated.

2 answers

3


A simple break would even solve the problem, but your code has several others that I reported in the comments. Faced with this, I would suggest a different approach, which solves the problem of repeaters and others that the code has:

public static void main(String[] args) {

    int[] ra = new int[10];

    int indice = 0;

    boolean cadastrado = false;

    do {

        int cadastroRa = Integer.valueOf(JOptionPane.showInputDialog("Digite o RA que deseja cadastrar:"));

        if (cadastroRa <= 0) {
            JOptionPane.showMessageDialog(null, "O número deve ser maior que zero...");
        } else if (existe(cadastroRa, ra)) {
            JOptionPane.showMessageDialog(null, "O RA digitado já esta cadastrado.");
        } else if (indice < ra.length){

            ra[indice] = cadastroRa;
            indice++;
            cadastrado = true;
            JOptionPane.showMessageDialog(null, "RA " + cadastroRa + " cadastrado com sucesso.");

        }

    } while (!cadastrado);

}

public static boolean existe(int cadastroRa, int[] listaRa) {

    boolean existe = false;

    for (int i = 0; i < listaRa.length; i++) {

        if (cadastroRa == listaRa[i]) {
            existe = true;
            break;
        }
    }

    return existe;
}

See that the method existe() has the sole purpose of checking whether the value already exists in the array, and returns true or false if it is found.

Already the registration in the array, the control is done by the variable indice, thus avoiding filling an already filled array value or a position larger than the array size.

  • I had not studied the possibility of creating a method for this, I believed I could perform the validation only in the if. Helped a lot, man, Thank you.

  • 1

    @P.Pires of course can and should! This helps you better organize your code, as well as separate responsibilities =)

  • Definitely saved some lines.

0

if (Arrays.stream(ra).anyMatch(xh -> xh == cadastroRa))

Your final code will look like this:

for (int i = 0; i < ra.length; i++) {
    int cadastroRa = Integer.valueOf(JOptionPane.showInputDialog("Digite o RA que deseja cadastrar:"));

    if (cadastroRa == 0) {
        JOptionPane.showMessageDialog(null, "O número deve ser maior que zero...");
    } else {                            
        if (Arrays.stream(ra).anyMatch(xh -> xh == cadastroRa)) {
            JOptionPane.showMessageDialog(null, "O RA digitado já esta cadastrado.");
        } else {
            ra[i] = cadastroRa;
            JOptionPane.showMessageDialog(null, "RA " + cadastroRa + " cadastrado com sucesso.");
        }
    }
}

Browser other questions tagged

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