简体   繁体   English

为什么这个方法调用不起作用?

[英]Why won't this method call work?

I'm creating a method to take an input by a user and validate it to make sure it's correct. 我正在创建一种方法来接受用户的输入并验证它以确保它是正确的。 If it's correct it will call a method and input the user input in to it. 如果它是正确的,它将调用一个方法并输入用户输入。 But for some reason, the method call is not working. 但由于某种原因,方法调用不起作用。 It doesn't produce any errors, it just simply doesn't do it. 它不会产生任何错误,它只是没有做到。 I placed a print statement at the end of the code to make sure it actually reaches there and it does, but for some reason it's just not calling the method like it's supposed to. 我在代码的末尾放置了一个print语句,以确保它实际到达那里并确实如此,但由于某种原因,它只是没有像它应该的那样调用方法。 The other method works fine if I call it by itself and input a string via the parameters. 如果我自己调用它并通过参数输入一个字符串,另一种方法可以正常工作。

The code is: 代码是:

public void getGetScheduledShowByFilmInput()////new - omar////
{
    BufferedReader reader;
    reader = new BufferedReader(new InputStreamReader(System.in));

    String filmInput;
    filmInput = "";

    boolean foundFilm;
    foundFilm = false;

    System.out.println("Here is a list of films that are currently showing:");
    for(Film film : films){
        System.out.println(film.getFilmName());
    }

    System.out.println("");
    System.out.println("Please type the film name that you wish to view the corresponding shows for and press enter.");
    System.out.println("Type 'exit' and press enter to exit this process.");

    while(foundFilm == false){
        try{
            filmInput = reader.readLine();
        }
        catch (IOException e){
            System.out.println("Error");
        }

        //If user enters "exit" then return.
        if(filmInput.equals("exit")){
            return;
        }

        //Check to see if the film name input by the user corresponds to any film showing.
        for(Film film : films){
            if(film.getFilmName() == filmInput){
                foundFilm = true;
                break;
            }
        }

        if(foundFilm = true){
            System.out.println("Film found.");
        }
        else{
            System.out.println("The film name you entered has not been recognised.  Please try again.");
        }
    }

    //Call the function and input the film name input by the user.
    getScheduledShowsByFilm(filmInput);  ////This is the code that seems to be the problem.
    System.out.println("reached bottom");

}

and the second method is: 第二种方法是:

 public void getScheduledShowsByFilm(String inputFilmName)
{
    ArrayList<Show> scheduledShows;
    scheduledShows = new ArrayList<Show>();
    for(Film film : films){
        if(inputFilmName == film.getFilmName()){
            for(Schedule schedule : schedules){
                scheduledShows.add(schedule.getShowsOfFilm(film));
                if(scheduledShows.get(scheduledShows.size() - 1) == null){
                    scheduledShows.remove(scheduledShows.size() - 1);
                }
            }
        }
    }

    for(Show show : scheduledShows){
        System.out.println("**********************************");
        show.getShowDetails();
        System.out.println("**********************************");
    }
}

The second method works perfectly when I call it on its own and enter parameters manually though. 当我自己调用它并手动输入参数时,第二种方法可以正常工作。

It's probably something extremely simple that I'm not understanding! 这可能是非常简单的,我不理解! haha, thank you for your help :) 哈哈,谢谢你的帮助:)

foundFilm can never be false because you always assign true to it: foundFilm永远不会是假的,因为你总是赋予它真实:

 if(foundFilm = true){
            System.out.println("Film found.");
 }

try changing it to this: 尝试将其更改为:

if(foundFilm)
{
   System.out.println("Film found.");
}

In getGetScheduledShowByFilmInput() and getScheduledShowsByFilm(String) avoid doing string comparison using the equality operator (==). getGetScheduledShowByFilmInput()getScheduledShowsByFilm(String)避免使用相等运算符(==)进行字符串比较。 The == operator tests for object equality, but you want to test whether two strings contain the same sequence of characters. ==运算符测试对象相等性,但是您想测试两个字符串是否包含相同的字符序列。 Therefore, use equals instead: 因此,请使用equals代替:

    //Check to see if the film name input by the user corresponds to any film showing.
    for(Film film : films){
        if(film.getFilmName().equals(filmInput)){
            foundFilm = true;
            break;
        }
    }

and

for(Film film : films){
    if(inputFilmName.equals(film.getFilmName())){
        for(Schedule schedule : schedules){
            scheduledShows.add(schedule.getShowsOfFilm(film));
            if(scheduledShows.get(scheduledShows.size() - 1) == null){
                scheduledShows.remove(scheduledShows.size() - 1);
            }
        }
    }
}

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

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