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.
– user28595
Above the for I have an if for validation if the array is fully filled.
– P.Pires
So add to the question. I’m trying to elaborate a suggestion, but with parts missing gets complicated.
– user28595