Search code examples
javabluejunreachable-statement

Unreachable Statement in Java


I am working in BlueJ for my university course, and I have been set a basic assignment where we need the user to input certain information about a DVD, such as director, name, running time, etc.

I am coding in Java, and used Genio too. The following code is how I got and set the information within the variables;

 public void getDVDInfo() {
    //Ask for info
    System.out.println("Please Enter the Film Info:");
    System.out.println("Film Name: ");
    System.out.println("Film Director: ");
    System.out.println("Lead Actor/Actress: ");
    System.out.println("Running Time: ");
}

public void setDVDInfo() {
    //set user input to variables
    filmName = Genio.getString();
    director = Genio.getString();
    leadActor = Genio.getString();
    runTime = Genio.getInteger();
}

This all works according to the compiler I used within BlueJ, but when i coded the function to return the information to the user, like so;

  public String seeDVDInfo() {
    return filmName;
    return director;
    return leadActor;
}

public int seeRunTime() {
    return runTime;
}

it came up with a compiler error at return director; that is was an unreachable statement. i don't understand where this is coming from, it all looks right, can anyone help me?

Thanks in advance xx


Solution

  • If, this, is really the method in your code:

    public String seeDVDInfo() {
        return filmName;
        return director;
        return leadActor;
    }
    

    Then, the problem is that a method is finished with the a return statement. It will exit the method with the provided value.

    You have different ways to solve the issue, depending on the information you want to provide/use:

     public String seeDirector() {
         return director;
     }
     public String seeFilmName() {
         return director;
     }
     public String leadActor() {
         return director;
     }
    

    Taking into account that java is an OOP language, I would suggest creating a class Film that contains/ encapsulates this info that can be returned. So actually it will look something like this:

    public class Film {
        private String director;
        private String filmName;
        private String leadActor;
    
        public Film(String director, String filmName, String leadActor) {
            this.director = director;
            this.filmName = filmName;
            this.leadActor = leadActor;
        }
    
        public String getFilmName() {
             return filmName;
        }
    
        public void setFilmName(String filmName) {
             this.filmName = filmName;
        }
    
        public String getDirector() {
             return director;
        }
    
        public void setDirector(String director) {
             this.director = director;
        }
        public String getLeadActor() {
             return leadActor;
        }
    
        public void setLeadActor(String leadActor) {
             this.leadActor = leadActor;
        }
    }