0

im having great trouble with my program. It's very simple, and i've completed it to the last step. Now though, I cant get the forloop to work and print out the name of movies.

insertMov creates three movie objects. The objects contain name, language and rating. I think it could be a problem with my scanner but im not sure. Thanks!

`import java.util.Scanner;
public class MovieRecommendation{
Movie  [] movarr  = new Movie[3];
Scanner scanner = new Scanner(System.in);
public void insertMov() {
    movarr[0] = new Movie   ("Avengers", "english", 2013, "pg" );
    movarr[1] = new Movie   ("Ironman", "english", 2008, "pg" );
    movarr[2] = new Movie   ("fantastic 4", "english", 2005, "pg" );
    }

public String main() {
    insertMov();

    System.out.println("Hey, I hear you want to watch a movie.");
    System.out.println("what language do you want the movie to be in?");
    String langInp = scanner.nextLine();
    System.out.println("Thanks! Now, what rating would you like your movie to be?");
    String ratInp  = scanner.nextLine();


    System.out.println("Awesome :D Here are the movies Chosen for you");

    for (int i = 0; i < movarr.length; i++ ){

        if ( langInp == movarr[i].getLanguage() && ratInp == movarr[i].getRating()) {
            System.out.println(movarr[i].getName());
        }

    }

    return ("Feel free to use again");
}

}`

  • The problem is that in java, you need to use `.equals()` method to compare strings, not `==`. Have a look at http://www.programmerinterview.com/index.php/java-questions/java-whats-the-difference-between-equals-and/ for an explanation. – Sci Prog Apr 01 '16 at 03:26
  • There is no problem with for loop problem with your condition in if i.e. ==; Because == only check references in Java if you want to compare values you should use `string.equals(anotherString)` method and I am sure `System.out.println(movarr[i].getName());` will be executed. – Chetan S. Choudhary Mar 09 '17 at 06:29

0 Answers0