1

I have to make an application that manages the billboard of a cinema.I have a standard data that contains, among other data, the title of the movie. I also have a session type data that contains int capacity, localtime time, localdate date and the movie. I have to make a method that eliminates the session passing as parameters the title of the movie, the date and the time of the session. The method fails me in the if inside the for. I do not know what I'm doing wrong. Could it be because of how I compare data of type localdata and local time?

public boolean quitarSesion(String tituloEliminar, LocalDate fechaEliminar, LocalTime horaEliminar)
    {
        int count=0;
        for(Sesion sesion: sesiones){
            if(sesion.pelicula.titulo==tituloEliminar && sesion.fecha==fechaEliminar && sesion.hora==horaEliminar){
                sesiones.remove(sesion);
                System.out.println("Sesion eliminada correctamente");
                count++;
                
            }
           
        if(count==0){
            System.out.println("No se ha eliminado ninguna sesion");
            return false;         
        
        }
        else{
            System.out.println("Sesion eliminada correctamente");
            return true;  
        }
            
    }

1 Answers1

4

Multiple issues with your code.

sesion.pelicula.titulo==tituloEliminar

== compares 'references', which is not what you want, you want to check if the strings are equal. == does not do that. You're looking for sesion.pelicula.titulo.equals(tituloEliminar).

sesiones.remove(sesion);

you can't remove from sesiones whilst looping over sesiones. Remember what you want to remove, and then do it at the end. Alternatively, don't use for(:) syntax, instead, grab an iterator, and when you found the one you want to remove, call .remove() on it (call var it = sesiones.iterator();, replace the for loop while a while (it.hasNext()), get the session with it.next(), and finally if your if says: This one needs to be gone, call it.remove().

How to compare LocalDate and LocalTime

The == vs .equals thing, which applies to all non-primitive types (so, for LocalDate and LocalTime you also need to use .equals, don't use ==, that doesn't do what you think it does). Otherwise, no problems here.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • Thanks! the one tested with is the same and has worked. To remove the sessions I have created a list where I store the matches and I have used removeall. Does it work the same? – Catacroker May 10 '21 at 00:28
  • Yup, that will work :) I don't know what 'the one tested with is the same and has worked' means, but if you mean: Hey, `==` seems to work for me - that's fake. Replace that code, it'll blow up on you later. And read up on what 'reference identity' vs 'value identity' is. – rzwitserloot May 10 '21 at 02:00
  • Sorry for my English. It is not a good idea to use the translator. I wanted to say that I used equals and it has worked. :) – Catacroker May 10 '21 at 02:11
  • Nice :) Good luck on the project! – rzwitserloot May 10 '21 at 02:23
  • “The `==` vs `.equals` thing, which applies to **all** non-primitive types…” (except enums) – VGR May 10 '21 at 19:19