English 中文(简体)
为什么赢得这一法典承认这两个变量是相同的?
原标题:Why won t this code recognise that these two variables are the same?
  • 时间:2012-04-27 00:30:13
  •  标签:
  • java
  • compare

Hey I m trying to take a String input from the user (in this case a name of a film e.g. Good Burger). I have an arraylist of class Film from which I m iterating through. On each iteration a method in the instance of class Film is called which returns a String of the film name. When I m comparing these two, it doesn t seem to recognise that they are equal and I can t figure out why.

此处为参考和比较这两种意见的代码:

用户对电影的投入。

    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.");
        }

这里是班级守则。 电影:

public String getFilmName()
{
    return filmName;
}

如果你注意到那里任何流ogue的印刷说明,我检查这部守则是正确的。

任何帮助都受到高度赞赏! 成就

最佳回答

You appear to set filmName to null at the start, then you never actually change it (by calling getFilmName(), for example). Hence, it will be null for the purposes of comparison.

我怀疑你可能需要这样的东西:

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 + " ")

在<代码>(输入FilmName. Equals(filmName){行文之前,请见为什么不均等。 Oh注意到单一报价。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签