The execution of the program does not enter the foreach

0

Good I am learning to use the foreach loop in java but I do not get my program inside, I hope you can give me some clue, thanks.

import java.util.ArrayList;import java.util.Scanner;
public class Taller {

private ArrayList<Vehiculo> listaVehiculos2;
public Taller() {//C

    listaVehiculos2=new ArrayList<Vehiculo>();
}
public void altaVehiculo(Scanner in) {  
    int codigo; //A
    String nombre;
    boolean existe = false; 

        System.out.println("Introduce un codigo"); 
        codigo=in.nextInt();

    System.out.println("Has introducido "+codigo);

for ( Vehiculo vehiculo : listaVehiculos2) {    
    if (vehiculo.getCodigo()==codigo); {
        existe = true;
    }
    if (existe) {
        System.out.println("ya existe");
    } else {
        System.out.println("Alta de vehiculo");
                //voy pidiendo datos del objeto
        nombre=in.next();
        System.out.println("Se ha dado de alta un vehiculo");   
    }   
}




}

This is the class of the vehicle

public class Vehiculo {
//A
    int codigo;
    String nombre;


    public Vehiculo(int codigo, String nombre) {
        //this.codigo = cont;
        this.codigo = codigo;
        this.nombre = nombre;

    }



    public int getCodigo() {
        return codigo;
    }

    public void setCodigo(int codigo) {
        this.codigo = codigo;
    }

    @Override
    public String toString() {
        return "Vehiculo [codigo=" + codigo + ", nombre=" + nombre + "]";
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

}
    
asked by David Palanco 18.04.2018 в 21:35
source

1 answer

2

Basically you already have your program, you only need two things:

  • Assign data to a new vehicle.
  • Add the new vehicle to the list.
  • 1. Assign data to a new vehicle:

    In the code you published, you have all the necessary data to create a new vehicle, however, you have not created the instance as such of the vehicle, that is, there is no "physically" said vehicle. For this, you have created a Boolean variable, which checks whether the vehicle exists or not, what you must do is create the new instance if it does not exist:

    for ( Vehiculo vehiculo : listaVehiculos2) {    
        if (vehiculo.getCodigo() == codigo); {
            existe = true;
        }
    
        if (existe) {
            System.out.println("ya existe");
        } else {
            System.out.println("Alta de vehiculo");
    
            nombre = in.next(); // Ingresas el nombre del vehiculo
    
            Vehiculo nuevo = new Vehiculo(codigo, nombre); // el codigo ya lo habias ingresado
    
            System.out.println("Se ha dado de alta un vehiculo");   
        }   
    }
    

    In this way, the vehicle would already exist "physically" however, you have not added it to the list yet.

    2. Add the new vehicle to the list.

    To finish, you only need to call the add of your list:

    listaVehiculos2.add(nuevo);
    

    You can also add it more quickly as follows:

    listaVehiculos2.add(new Vehiculo(codigo, nombre));
    

    Your code should look like this:

    for ( Vehiculo vehiculo : listaVehiculos2) {    
        if (vehiculo.getCodigo() == codigo); {
            existe = true;
        }
    
        if (existe) {
            System.out.println("ya existe");
        } else {
            System.out.println("Alta de vehiculo");
    
            nombre = in.next(); // Ingresas el nombre del vehiculo
    
            listaVehiculos2.add(new Vehiculo(codigo, nombre));
    
            System.out.println("Se ha dado de alta un vehiculo");   
        }   
    }
    

    WARNING

    If you have already tried the code, you will notice that it is common for you to throw an exception, specifically ConcurrentModificationException , which indicates that it is not possible to modify (in this case, add a new element) to a collection (list) while that list is being iterated.

    To avoid this, you have two options:

  • Take advantage of your Boolean variable to verify if the vehicle exists, and carry out operations out of for .
  • Create a copy of your list when iterating.
  • I'll explain a little:

    In case number 1, you could take advantage of your variable to only verify if it exists or not, something similar to this:

    for ( Vehiculo vehiculo : listaVehiculos2) {    
        if (vehiculo.getCodigo() == codigo); {
            existe = true;
            break; // detiene la ejecución del bucle para continuar
        }
    }
    
    if (existe) {
        System.out.println("ya existe");
    } else {
        System.out.println("Alta de vehiculo");
    
        nombre = in.next(); // Ingresas el nombre del vehiculo
    
        listaVehiculos2.add(new Vehiculo(codigo, nombre));
    
        System.out.println("Se ha dado de alta un vehiculo");   
    }
    

    This way you get the expected result.

    In case number 2, you could create a copy of your list at the time of iterating, however, this would not be recommended in case the list is too large, the advantage is that you could keep the same code without having to modify it a lot:

    for ( Vehiculo vehiculo : new ArrayList<>(listaVehiculos2)) { // Aqui se crea la copia
        if (vehiculo.getCodigo() == codigo); {
            existe = true;
        }
    
        if (existe) {
            System.out.println("ya existe");
        } else {
            System.out.println("Alta de vehiculo");
    
            nombre = in.next(); // Ingresas el nombre del vehiculo
    
            listaVehiculos2.add(new Vehiculo(codigo, nombre));
    
            System.out.println("Se ha dado de alta un vehiculo");   
        }   
    }
    
        
    answered by 18.04.2018 / 22:29
    source