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.
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:
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 🙂You appear to set
filmNameto null at the start, then you never actually change it (by callinggetFilmName(), for example). Hence, it will be null for the purposes of comparison.I suspect you may need something like: