Array identification problem

0

I was doing a java method to which I passed a license plate for a truck. This method, when I found a truck with that license plate, was going to record its arrival time, and it was going to mark if it had arrived on time or not (this last part works, it is merely explanatory). The problem comes when I use this method, never find the truck with the indicated license plate. My method code is:

public void llegaCamion(String matricula) {
    LocalDate date=LocalDate.now();
    int dow=date.getDayOfWeek().ordinal();
    if(dow==0) {
        for(int i=0;i<planificadosMonday.size();i++) {
            if(planificadosMonday.get(i).getmatricula()==matricula) {
                planificadosMonday.get(i).setHoraDesc(LocalTime.now());
            }
        }
        System.out.println("Lo siento, no hemos encontrado el camión");
    }else if(dow==1) {
        for(int i=0;i<planificadosTuesday.size();i++) {
            if(planificadosTuesday.get(i).getmatricula()==matricula) {
                planificadosTuesday.get(i).setHoraDesc(LocalTime.now());
            }
        }
        System.out.println("Lo siento, no hemos encontrado el camión");
    }else if(dow==2) {
        for(int i=0;i<planificadosWednes.size();i++) {
            if(planificadosWednes.get(i).getmatricula()==matricula) {
                planificadosWednes.get(i).setHoraDesc(LocalTime.now());
            }
        }
        System.out.println("Lo siento, no hemos encontrado el camión");
    }else if(dow==3) {
        for(int i=0;i<planificadosThurs.size();i++) {
            if(planificadosThurs.get(i).getmatricula()==matricula) {
                planificadosThurs.get(i).setHoraDesc(LocalTime.now());
            }
        }
        System.out.println("Lo siento, no hemos encontrado el camión");
    }else if(dow==4) {
        for(int i=0;i<planificadosFri.size();i++) {
            if(planificadosFri.get(i).getmatricula()==matricula) {
                planificadosFri.get(i).setHoraDesc(LocalTime.now());;
                System.out.println("El camion ha llegado");
            }

        }
        System.out.println("Lo siento, no hemos encontrado el camión");

    }else {
        System.out.println("Lo siento, no hay camiones asignados el fin de semana");
    }
}

As you can guess, it is a nested if depending on the day it is in one list or another (the trucks are already distributed by days correctly in other methods, and the lists contain all the elements they must). The case is that in my main, put the license plate that you put, you can not find the truck even if it is on the list.

I pass my code of the main

public static void main(String[]args) throws IOException {
    Calendar c=Calendar.getInstance();
    LocalDate date=LocalDate.now();
    DayOfWeek dow=date.getDayOfWeek();
    ListaCamiones l=new ListaCamiones();
    if(dow.ordinal()==DayOfWeek.MONDAY.ordinal()) {
        l.getPlanif("M");
        l.muestraCamionesPlanif("M");
    }//SEGUIR CON LOS DEMAS DIAS
    else if(dow.ordinal()==DayOfWeek.TUESDAY.ordinal()) {
    l.getPlanif("T");
    l.muestraCamionesPlanif("T");
    }
    else if(dow.ordinal()==DayOfWeek.WEDNESDAY.ordinal()){
        l.getPlanif("W");
        l.muestraCamionesPlanif("W");

    }else if(dow.ordinal()==DayOfWeek.THURSDAY.ordinal()) {
        l.getPlanif("TH");
        l.muestraCamionesPlanif("TH");

    }else if(dow.ordinal()==DayOfWeek.FRIDAY.ordinal()) {
        l.getPlanif("F");
        l.muestraCamionesPlanif("F");

    }else {
        System.out.println("Error");
    }

    ArrayList<Camion>cam=(ArrayList<Camion>) l.getPlanificadosFri();
    for(int i=0;i<cam.size();i++) {
        System.out.println(cam.get(i).getmatricula());
    }
    l.muestraCamionesPlanif("F");
    String matricula="opi1928";
    l.llegaCamion(matricula);

}

In fact, in: for(int i=0;i<cam.size();i++) { System.out.println(cam.get(i).getmatricula()); }

I can see the license plates, but afterwards when I use the method, it arrives at Bus (), but can not find it.

    
asked by Adrián 14.12.2018 в 11:09
source

1 answer

1

In Java the method to compare two strings is equals , not == , so all comparisons will thus fail:

if(planificadosTuesday.get(i).getmatricula()==matricula) {

You should write them like this:

if ( planificadosTuesday.get(i).getmatricula().equals(matricula) ) {

That is certainly the reason for the error.

  

NOTE ON OPTIMIZATION:

     

A very repetitive code is appreciated in your question. Considers   implement a clearer structure for these cases, such as   a block switch ... case , or you can also implement something else   advanced as data structures that store the information in pairs   key / value, looking for information in these structures according to the   key. So you will get rid of so many if ... elseif ... else and   you will have a more understandable, clearer, more portable code, more   flexible.

    
answered by 14.12.2018 в 11:45