How to find a string within a list

3

In this code, what I need is to compare the movie string to the attribute of all the objects in the Movie class, and if it finds it returning a Boolean, but somehow it does not, can you help me and see what is wrong? .

Thanks

   public class Pelicula{

        private String pelicula;

        public Pelicula(int idPelicula, String pelicula) throws Exception{
            super();
            this.idPelicula = idPelicula;
            this.setPelicula(pelicula);
        }

        public void setPelicula(String pelicula) throws Exception 
        {
            boolean verificar = verificarPelicula(pelicula);
            if (verificar == false) throw new Exception("Error: Pelicula existente");
            this.pelicula=pelicula;
        }

        public boolean verificarPelicula(String pelicula)
        {
            List<Pelicula> listado = new ArrayList<>(); //intente con new ArrayList<Pelicula>

            for(int cantPel = 0 ; cantPel <listado.size();cantPel++)
           {
                if (pelicula.equals(listado.get(cantPel).pelicula))//tambien intente con .getPelicula()
                {
                    return false;
                }
            }
            return true;
    }

            public String getPelicula(){
              return pelicula;
            }
    }
    
asked by Ariel Nicolas Heredia 26.05.2017 в 20:37
source

5 answers

2

You should have another class that stores your movies or in the same class put a list of movies. Finally you decide how to do, but to find a movie you can do it like this:

    list<Pelicula> peliculas

    public boolean verificarPelicula(String peliculaBuscada) {
        peliculas.stream().anyMatch(pelicula -> pelicula.getPelicula() == peliculaBuscada);
    }

I explain the code. We have a list of movies and I use the stream () method, with this we can use lambda, if you do not know what it is, I recommend you investigate because it solves many things and is very good to use. Then I use the anyMatch method, which basically what it does is to return a Boolean from the block that is specified in anyMatch, and inside anyMatch you use any parameter (in this case I put a movie, but it can be x, y) and if you realize in the parameter you can use the movie methods and the last thing you do is find the movie you want. The stream (). AnyMatch () would be something similar with a for:

    public boolean verificarPelicula(String peliculaBuscada) {
            boolean existe = false;
            for(Pelicula pelicula : peliculas) {
                    if(pelicula.getPelicula() == peliculaBuscada)
                            existe = true;
            }
            return existe;
    }
    
answered by 27.05.2017 в 03:17
1

I think you have a problem with your design, your class has been doing it for a long time, it should not check itself if it contains more movies.

What I recommend is that you overwrite the hashCode and equals method of the movie class using only your movie attribute.

so you can get from the collection listado.contains() if it exists.;

    
answered by 26.05.2017 в 20:46
0

For what I see you want to have as a set of movies, and go adding movies to this set without having films with duplicate id, in that case I would have two classes, the class Pelicula that would have the id of the movie and the text, and the% Shelf , where the movies would be grouped.

The problem with the code is that you try to do these steps in a single class and in List<Pelicula> listado = new ArrayList<>(); you generate a set of empty movies every time you want to verify if a new movie is unique

An example in java:

    public class Movie {
        private String title;
        private int id;
        public Movie(int id, String title) {
            this.title = title;
            this.id = id;
        }
        public int getId() {
            return this.id;
        }
    };

    public class Shelf {
        private List<Movie> arr;
        private boolean alreadyExist(Movie movie) {
            for (int i = 0; i < arr.size(); i++) {
                if (arr[i].getId() == movie.getId()) {
                    return true;
                }
            }
            return false;
        }
        public void addMovie(Movie movie) {
            if (!alreadyExist(movie)) {
                // add movie to arr
            }
        }
    };
    
answered by 26.05.2017 в 20:45
0

You are comparing with a list of objects Pelicula that does not contain elements , you create listado but it does not contain elements, it does not even enter for due to this situation since listado.size() has value of 0:

public boolean verificarPelicula(String pelicula)
{
    List<Pelicula> listado = new ArrayList<>(); //intente con new ArrayList<Pelicula>

    for(int cantPel = 0 ; cantPel <listado.size();cantPel++)
   {
        if (pelicula.equals(listado.get(cantPel).getPelicula())){
            return false;
        }
    }
    return true;
}

The comparison is not made, for this reason you will always get the value true .

As a suggestion you could modify your method so that it receives the list of Objects and carries out the desired verification:

  public boolean verificarPelicula(String pelicula, List<Pelicula> listado)
        {    
            for(int cantPel = 0 ; cantPel <listado.size();cantPel++)
           {
                if (pelicula.equals(listado.get(cantPel).pelicula))
                {
                    return false;
                }
            }
            return true;
    }
    
answered by 26.05.2017 в 20:47
-1

When you do this your list is empty

List<Pelicula> listado = new ArrayList<>(); //intente con new ArrayList<Pelicula>

As indicated in the comments, since the List is empty you would not enter the for to make the comparison and for this reason you will always get the value true .

    
answered by 26.05.2017 в 20:47