0

I get an error message cannot convert from String to Movie. Why do I get this error message what should I be returning in the first if statement. Should I call the class Movie?

public Movie findMovieByName(String inTitle) {
    for (Movie movies : movieArray)
        if (inTitle == movies.getTitle())
            return movies.getTitle();
        else
            return null;
5
  • use inTitle.equals(movies.getTitle()) instead. Commented Feb 10, 2013 at 18:19
  • it returns the movie title in the Movie class this is in a different class Commented Feb 10, 2013 at 18:19
  • Considering your method, you need to return a Movie, not a String - also, if the first movie in your array does not match, you are going to return null, which is probably not what you need. Commented Feb 10, 2013 at 18:19
  • @vishalK then I get an error message cannot convert from boolean to movie? Commented Feb 10, 2013 at 18:20
  • You should return movies instead of movies.getTitle() Commented Feb 10, 2013 at 18:23

5 Answers 5

3

You could do:

public Movie findMovieByName(String inTitle) {
    for (Movie movie : movieArray) {
       if (inTitle.equals(movie.getTitle()) {
         return movie;
       }
    }

    return null;
}

Changes:

  • Use String.equals for comparing String content. == compares object references
  • Don't return null until you have checked every Movie

Side Note: A Map<String, Movie> could be used here instead if lookups are frequent.

Sign up to request clarification or add additional context in comments.

Comments

1

You have returned Title instead of movie

Comments

1

There are two main issues with your code:

The first one is here:

if (inTitle == movies.getTitle())

The == operator compares the instance of the String inTitle with the instance of the String returned by movies.getTitle(). But what you want to do is compare the characters contained in the two instances and see if they are the same. For this you should write:

if (inTitle.equals(movies.getTitle()))

The second problem is that your findMovieByName method should return an instance of Movie, but you are returning a String:

return movies.getTitle();

You should do the following instead:

return movies;

Comments

0
public Movie findMovieByName(String inTitle) {
    for (Movie movies : movieArray){
        if (inTitle.equals(movies.getTitle())){
            return movies.getTitle();
        }
    }
    return null;
}

As I can see from your code, movies.getTitle() either returns a Movie or a String object.

But with your error message, I am very sure you meant return movies instead of return movies.getTitle()

Comments

0

Assuming that movies.getTitle() returns a string, you can (and should) use:

inTitle.compareTo(movies.getTitle()) == 0

And, of course, what's the point on returning the same title you are entering? Either you can return a true value, meaning the value of inTitle was found, or return the movie (which is what the definition of your procedure says. So, your procedure will look as follows:

public Movie findMovieByName(String inTitle) {
   for(Movie movies : moviesArray) {
       if (inTitle.compareTo(movies.getTitle()) == 0)
           return movies;
       else
           return null;
   }
}

You can also use compareToIgnoreCase if you want your comparison to be case-insensitive.

See http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#compareTo(java.lang.String) and http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#compareToIgnoreCase(java.lang.String)

Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.