Error: Variable auxi is already defined in method main (String [])

0

I continue with my programming studies and I have generated a problem in my code, it tells me that the auxi variable is already defined in one method, but the intent use to invoke the arrayList, the funny thing is that I do not throw that error with the previous variables, that I use them in the same way.

I also suppose that because of this error, it also generates an error in the line Bus bs = new Bus(Patente, Capacidad, CiudadSalida, CiudadDestino, HoraSalida, tChofer, tAzafata, tAuxiliar);

I would be very grateful if you correct me in what I am wrong

import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;



public class ParteI {
    private static TipoLicencia TL;
    static Scanner sc = new Scanner(System.in);
    static ArrayList<Cliente> Client = new ArrayList<>();
    static ArrayList<Chofer> Driver = new ArrayList<>();
    static ArrayList<Azafata> Azaf = new ArrayList<>(); 
    static ArrayList<Auxiliar> Auxil = new ArrayList<>();
    static ArrayList<Bus> Buus = new ArrayList<>();
    static String Bsc;

public static void main(String[] args) {


    boolean salir = false;
    int opcion, Edad, Capacidad;
    String Rut, Nombre, Apellido, Telefono, Telefono_E, Patente, CiudadSalida, CiudadDestino, HoraSalida;
    Chofer tChofer;
    Azafata tAzafata;
    Auxiliar tAuxiliar;

    while(!salir) {      


        System.out.println("Menu Opciones");
        System.out.println("[1] Agregar");
        System.out.println("[2] Buscar");
        System.out.println("[3] Reserva");
        System.out.println("[4] Ver");
        System.out.println("[5] Salir");

        try{



            System.out.println("=== Ingrese Numero de Opcion ===");
            opcion=sc.nextInt();

            switch (opcion){
                case 1:
                    System.out.println("Eliga que desea A G R E G A R");
                    System.out.println("[1] Cliente");
                    System.out.println("[2] Chofer");
                    System.out.println("[3] Azafata");
                    System.out.println("[4] Auxiliar");
                    System.out.println("[5] Bus");
                    opcion=sc.nextInt();
                    switch (opcion){
                        case 1://------------------------------------------- Agregar Cliente
                            System.out.println("Agregar un CLIENTE");
                            String Correo;
                            System.out.println("Rut: ");
                            Rut = sc.next();
                            System.out.println("Nombre: ");
                            Nombre = sc.next();
                            System.out.println("Apellido: ");
                            Apellido = sc.next();
                            System.out.println("Telefono: ");
                            Telefono = sc.next();
                            System.out.println("Correo: ");
                            Correo = sc.next();

                            Cliente clie = new Cliente(Correo, Rut, Nombre, Apellido, Telefono);
                            Client.add(clie);

                            break;

                        case 2://------------------------------------------- Agregar Chofer ########################
                            System.out.println("Agregar Chofer");
                            System.out.println("Seleccione tipo de licencia");
                            System.out.println("[1] A1");
                            System.out.println("[2] A2");
                            System.out.println("[3] A3");
                            System.out.println("[4] A4");
                            System.out.println("[5] A5");
                            System.out.println("[6] B");
                            System.out.println("[7] C");
                            System.out.println("[8] D");
                            System.out.println("[9] E");
                            System.out.println("[10] F");
                            opcion=sc.nextInt();
                            if (opcion == 2){
                                System.out.println("Rut: ");
                                Rut = sc.next();
                                System.out.println("Nombre: ");
                                Nombre = sc.next();
                                System.out.println("Apellido: ");
                                Apellido = sc.next();
                                System.out.println("Telefono: ");
                                Telefono = sc.next();

                                Chofer cho = new Chofer(TipoLicencia.A2, Rut, Nombre, Apellido, Telefono);
                                Driver.add(cho);
                            }else{
                                System.out.println("Lo sentimos, no requerimos un Chofer con este tipo de Licencia");
                            }

                            break;

                        case 3://------------------------------------------- Agregar Azafata
                            System.out.println("Agregar Azafata");
                            System.out.println("Edad");
                            Edad = sc.nextInt();
                            if (Edad >= 21){
                                System.out.println("Rut: ");
                                Rut = sc.next();
                                System.out.println("Nombre: ");
                                Nombre = sc.next();
                                System.out.println("Apellido: ");
                                Apellido = sc.next();
                                System.out.println("Telefono: ");
                                Telefono = sc.next();

                                Azafata azafa = new Azafata(Edad, Rut, Nombre, Apellido, Telefono);
                                Azaf.add(azafa);
                            }else{
                                System.out.println("Lo sentimos, necesitamos alguien mayor a 21 años");
                            }

                            break;

                        case 4://------------------------------------------- Agregar Auxiliar
                            System.out.println("Agregar Auxiliar");
                            System.out.println("Rut: ");
                            Rut = sc.next();
                            System.out.println("Nombre: ");
                            Nombre = sc.next();
                            System.out.println("Apellido: ");
                            Apellido = sc.next();
                            System.out.println("Telefono: ");
                            Telefono = sc.next();
                            System.out.println("Telefono Emergencia: ");
                            Telefono_E = sc.next();

                            Auxiliar auxi = new Auxiliar(Telefono_E, Rut, Nombre, Apellido, Telefono);
                            Auxil.add(auxi);

                            break;

                        case 5://------------------------------------------- Agregar BUS #####################################################################################
                            System.out.println("Agregar Bus");
                            System.out.println("Capacidad");
                            Capacidad = sc.nextInt();
                            System.out.println("Patente");
                            Patente = sc.next();                                
                            System.out.println("Ciudad de Salida");
                            CiudadSalida = sc.next();
                            System.out.println("Ciudad de Destino");
                            CiudadDestino = sc.next();
                            System.out.println("Hora de Salida");
                            HoraSalida = sc.next();

                            for(Chofer cho : Driver){
                                System.out.println("Seleccione C H O F E R");
                                System.out.println("Ingrese R U T que desea B U S C A R");
                                Bsc = sc.next();
                                if(Bsc.equals(cho.getRut()));{
                                    System.out.println("RUT Encontrado");
                                    tChofer = cho;
                                    }
                                }

                            for(Azafata azafa : Azaf){
                                System.out.println("Seleccione A Z A F A T A");
                                System.out.println("Ingrese R U T que desea B U S C A R");
                                Bsc = sc.next();
                                if(Bsc.equals(azafa.getRut()));{
                                    System.out.println("RUT Encontrado");
                                    tAzafata = azafa;
                                    }
                                }

                            for (Auxiliar auxi : Auxil){ //Aqui es donde se genera el error <------------------------
                                System.out.println("Seleccione A U X I L I A R");
                                System.out.println("Ingrese R U T que desea B U S C A R");
                                Bsc = sc.next();
                                if(Bsc.equals(auxi.getRut()));{
                                    System.out.println("RUT Encontrado");
                                    tAuxiliar = auxi;
                                    }
                                }


                            Bus bs = new Bus(Patente, Capacidad, CiudadSalida, CiudadDestino, HoraSalida, tChofer, tAzafata, tAuxiliar);
                            Buus.add(bs);

                            break;

                        default:
                            System.out.println("Opcion no valida");

                    }//Fin switch2
                    break;

                case 2:
                    System.out.println("Eliga que desea B U S C A R");
                    System.out.println("[1] Cliente");
                    System.out.println("[2] Chofer");
                    System.out.println("[3] Azafata");
                    System.out.println("[4] Auxiliar");
                    System.out.println("[5] Bus");
                    opcion=sc.nextInt();
                    switch (opcion){
                        case 1:
                            System.out.println("Ingrese R U T que desea B U S C A R");
                            Bsc = sc.next();
                            for(Cliente clie : Client){
                                if(Bsc.equals(clie.getRut()));{
                                System.out.println("RUT Encontrado");
                                System.out.println("Nombre: " + clie.getNombre() + " Apellido: " + clie.getApellido() + " Correo: " + clie.getCorreo());
                                }
                            }
                            break;

                        case 2:
                            System.out.println("Ingrese R U T que desea B U S C A R");
                            Bsc = sc.next();
                            for(Chofer cho : Driver){
                                if(Bsc.equals(cho.getRut()));{
                                System.out.println("RUT Encontrado");
                                System.out.println("Nombre: " + cho.getNombre() + " Apellido: " + cho.getApellido() + " Tipo Licencia: " + cho.getLicencia());
                                }
                            }
                            break;
                        case 3:
                            System.out.println("Ingrese R U T que desea B U S C A R");
                            Bsc = sc.next();
                            for(Azafata azafa : Azaf){
                                if(Bsc.equals(azafa.getRut()));{
                                System.out.println("RUT Encontrado");
                                System.out.println("Nombre: " + azafa.getNombre() + " Apellido: " + azafa.getApellido() + " Edad: " + azafa.getEdad());
                                }
                            }
                            break;
                        case 4:
                            System.out.println("Ingrese R U T que desea B U S C A R");
                            Bsc = sc.next();
                            for(Auxiliar auxi : Auxil){
                                if(Bsc.equals(auxi.getRut()));{
                                System.out.println("RUT Encontrado");
                                System.out.println("Nombre: " + auxi.getNombre() + " Apellido: " + auxi.getApellido() + " Telefono Emergencia: " + auxi.getTelefono_E());
                                }
                            }
                            break;
                        case 5:

                            break;
                        default:
                            System.out.println("Opcion no valida");    
                    }
                    break;

                case 3:
                    System.out.println("Reserva");
                    break;

                case 4:
                    System.out.println("Eliga que desea V E R");
                    System.out.println("[1] Cliente");
                    System.out.println("[2] Chofer");
                    System.out.println("[3] Azafata");
                    System.out.println("[4] Auxiliar");
                    System.out.println("[5] Bus");
                    opcion=sc.nextInt();
                    switch (opcion){
                        case 1:
                            System.out.println("C L I E N T E");
                            for(Cliente clie : Client)
                                System.out.println("Nombre: " + clie.getNombre() + " Apellido: " + clie.getApellido() + " Rut: " + clie.getRut() + " Correo: " + clie.getCorreo());
                            break;
                        case 2:
                            System.out.println("C H O F E R");
                            for(Chofer cho : Driver)
                                System.out.println("Nombre: " + cho.getNombre() + " Apellido: " + cho.getApellido() + " Rut: " + cho.getRut() + " Tipo Licencia: " + cho.getLicencia());
                            break;
                        case 3:
                            System.out.println("A Z A F A T A");
                            for(Azafata azafa : Azaf)
                                System.out.println("Nombre: " + azafa.getNombre() + " Apellido: " + azafa.getApellido() + " Rut: " + azafa.getRut() + " Edad: " + azafa.getEdad());
                            break;
                        case 4:
                            System.out.println("A U X I L I A R");
                            for(Auxiliar auxi : Auxil)
                                System.out.println("Nombre: " + auxi.getNombre() + " Apellido: " + auxi.getApellido() + " Rut: " + auxi.getRut() + " Edad: " + auxi.getTelefono_E());
                            break;
                        case 5:
                            System.out.println("B U S");
                            break;
                        default:
                            System.out.println("Opcion no valida");    
                    }
                    break;

                case 5:
                    System.out.println("Usted a decidido salir1");
                    salir = true;
                    break;
                default:
                    System.out.println("Opcion debe ser entre 1 y 5");

        }// Fin switch

        }catch(InputMismatchException e){
            System.out.println("Debes introducir un numero");
            sc.next();
        }



    }//fin while


    System.out.println("Fin del menu"); 



}

}

    
asked by iFabio 31.10.2017 в 03:04
source

1 answer

0

Separate the scope of each declaration using {} keys for case to recognize the declaration independently:

int b = 4;
switch(b)
{
   case 1:{
     int a = 444;
   }
   break;
   case 2:{
      int a = 555;
   }
   break;
 }

The error it shows is because the scope of the variables corresponds to the switch , not the case . In other words: what you declare in the switch will be a general statement for the case , although it is not possible to access the variables declared in the previous cases.

    
answered by 31.10.2017 в 04:02