Loop to insert id not repeated in an ArrayList

3

This method basically checks whether an id is repeated and throws an error.

But it has an error: when entering a repeated id, I have more opportunities (ask for a new idea and the entry), but if I re-enter it again, it passes as true.

Does anyone know why or what mistake?

for (int i = 0; i < lista.size(); i++) {
   if (lista.get(i).getidGeneral() != idGeneral) {
    } else {
        while (lista.get(i).getidGeneral() == idGeneral) {

            try {
                throw new DuplicateVehicleException("La id ya esta en uso");
            } catch (DuplicateVehicleException e) {
                e.printStackTrace();
            }
            System.out.println("Introduce una nueva id:");
            idGeneral = lec.nextInt();
        }
    }
}
    
asked by Iron Man 06.07.2017 в 12:13
source

2 answers

1

You're throwing the exception whenever you enter.

for (int i = 0; i < lista.size(); i++) { 
  if (lista.get(i).getidGeneral() != idGeneral) {
    // Este if vacio no debería estar.
  } else { 
    while (lista.get(i).getidGeneral() == idGeneral) { 
      try { 
        // estas haciendo que siempre lanza esta excepción.
        throw new DuplicateVehicleException("La id ya esta en uso");  
      } catch (DuplicateVehicleException e) { 
        // Aquí siempre coges la excepción y se acabo el programa.
        e.printStackTrace();
      }
        System.out.println("Introduce una nueva id:"); 
        idGeneral = lec.nextInt(); 
    }
  }
}

I'll give you an example, with which, even if you find something repeated, keep going through the loop. Try this:

boolean flag = false;
for (int i = 0; i < lista.size(); i++) { 
  if(lista.get(i).getidGeneral() == idGeneral){
    flag = true;
  } else {
    System.out.println("Introduce una nueva id:"); 
    idGeneral = lec.nextInt(); 
  }
}
if(flag){
  throw new DuplicateVehicleException("Se ha intentado añadir una id en uso.");
}

In case you want everything to be interrupted in case you find a repeated one.

for (int i = 0; i < lista.size(); i++) { 
  if(lista.get(i).getidGeneral() == idGeneral){
    throw new DuplicateVehicleException("El id esta repetido");
  } else {
    System.out.println("Introduce una nueva id:"); 
    idGeneral = lec.nextInt(); 
  }
}

Anyway, it's a bit confusing, try to explain what you want to achieve, to see if I can help you better.

Greetings.

    
answered by 06.07.2017 / 12:37
source
3

I have already achieved the problem that I have been told is that follows the Array from where it is as if it had a "void" in the loop is not explained I am a novice .. the thing is that in a method and passing by paramtro the id does not make me that empty Thanks to everything!

private void compruebaId (int idGeneral) throws DuplicateVehicleException{

    for (int i = 0; i < lista.size(); i++) {
        if (lista.get(i).getidGeneral() == idGeneral) {
            throw new DuplicateVehicleException("La id ya esta en uso");

        }

    }
}
    
answered by 06.07.2017 в 13:07