简体   繁体   English

为什么此代码无法识别这两个变量相同?

[英]Why won't this code recognise that these two variables are the same?

Hey I'm trying to take a String input from the user (in this case a name of a film eg Good Burger). 嘿,我正在尝试从用户那里获取String输入(在这种情况下,是电影的名称,例如Good Burger)。 I have an arraylist of class Film from which I'm iterating through. 我有一个类Film的数组列表,我正在从中迭代。 On each iteration a method in the instance of class Film is called which returns a String of the film name. 每次迭代时,将调用类Film的实例中的方法,该方法返回电影名称的String。 When I'm comparing these two, it doesn't seem to recognise that they are equal and I can't figure out why. 当我比较这两者时,似乎并没有意识到它们是相等的,我也不知道为什么。

Heres the code that's taking the input and comparing the two: 这是接受输入并比较两者的代码:

//Get the films input by the user. //获取用户输入的电影。

    int numberOfFilmsCheck;
    numberOfFilmsCheck = 0;
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    Film tempFilm;
    System.out.println("Please enter the names of the films you wish to be in the new schedule");
    System.out.println("Press enter after each one.");

    while(numberOfFilmsCheck < numberOfFilms){
        boolean foundFilm;
        foundFilm = false;
        String inputFilmName = null;
        tempFilm = null;
        String filmName;
        filmName = null;
        try{
            inputFilmName = reader.readLine();
            System.out.println(inputFilmName);
        }
        catch (IOException e){
            System.out.println("Error");
        }

        for(Film film : films){
            film.printFilmName();
            if(inputFilmName.equals(filmName)){
                foundFilm = true;
                tempFilm = film;
                System.out.println("Found film name");
                break;
            }
        }

        if(foundFilm == true){
            newFilmsForSchedule.add(tempFilm);
            numberOfFilmsCheck++;
        }
        else{
            System.out.println("The film you entered has not been recognised.");
            System.out.println("Please enter the film name as shown above.");
        }

and here is the code in class Film that returns the film name: 这是Film类中返回影片名称的代码:

public String getFilmName()
{
    return filmName;
}

If you notice any rogue print statements in there that's just me checking the code is working correctly. 如果您发现其中有任何流氓打印语句,请检查代码是否正常工作。

Any help is much appreciated! 任何帮助深表感谢! Thanks 谢谢

The getFilmName() function looks like quite a useful one, but only if you end up calling it :-) getFilmName()函数看起来非常有用,但是只有在最终调用它时才:-)

You appear to set filmName to null at the start, then you never actually change it (by calling getFilmName() , for example). 您似乎在一开始filmName设置为null,但实际上您从未更改过它(例如,通过调用getFilmName() )。 Hence, it will be null for the purposes of comparison. 因此,出于比较目的,它将为null。

I suspect you may need something like: 我怀疑您可能需要以下内容:

for(Film film : films){
    film.printFilmName();
    filmName = film.getFilmName();               // <-- Added this.
    if(inputFilmName.equals(filmName)){          // <-- So that this works.
        foundFilm = true;
        tempFilm = film;
        System.out.println("Found film name");
        break;
    }
}

Try putting this code: 尝试输入以下代码:

System.out.println("'" + inputFilmName + "' vs '" + filmName + "'")

before the if(inputFilmName.equals(filmName)){ line and you WILL see why it is not equal. if(inputFilmName.equals(filmName)){行之前,您将看到为什么它不相等。 Oh notice the single quotes.. 哦,注意单引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM