1

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

4

7 回答 7

5

Problem is here:

public String seeDVDInfo() {
    return filmName;
    return director;
    return leadActor;
}
  • You cannot have multiple return statements in a method in Java.
  • Once you return filmName;, there is no way for other statements to execute, so it is throwing Unreachable Statement error.

See this thread to know why Unreachable Statement is an error in Java.

于 2013-10-10T11:34:43.990 回答
4

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;
    }
}
于 2013-10-10T11:34:53.050 回答
4

You have an error - the first return code will run and return the value of filmName, quitting the method, so other return calls cannot be reached.

A method can only return one object. You can use a complex object to hold some data and return it.

I suggest you create DvdInfo class:

public class DvdInfo {
    String filmName;
    String director;
    String leadActor;
// getters & setters...
}

And use it as the return type:

public DvdInfoseeDVDInfo() {
    new DvdInfo(filmName, director, leadActor);
}
于 2013-10-10T11:35:26.893 回答
4

The first statement in the seeDVDInfo method 'return filmName' returns the filmName variable and exits the seeDVDInfo method... Thus, the other 2 statements will never be run. Therefore, return director and return leadActor are 'unreachable'....

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

From the Java Tutorials:

A method returns to the code that invoked it when it completes all the statements in the method, reaches a return statement, or throws an exception (covered later), whichever occurs first.

http://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html

于 2013-10-10T11:36:17.730 回答
3

This is because once you return from the method , whatever code left after return statement will be unreachable , so it won't compile.

Here, i will try to explain you with 3 scenarios.

Scenario 1: For the Code,

public void helloWorld() {
  System.out.println("Hello");
  return;
  System.out.println("World");
 }

The Prited Line which marked as bold letter won't compile.

Scenario 2:

public void helloWorld() {
   System.out.println("Hello");
 if(true) {
      return;
  }
   System.out.println("World");
}

This Compiles Successfully. The most general logic in these rules are, code surrounded by a block is unreachable if the conditional expression deciding it evaluates to false and then the block is decided as unreachable. Then it becomes an error.

Only for ‘if’ construct the rule is different. The conditional expression is not evaluated and decided. Whatever may be the expression inside the if condition, it is not evaluated to decide if the code block is reachable and so we will not get error even if we have a constant value ‘false’ as conditional expression in if statement. This is treated special to give a facility for the developer. Developers can use a flag variable in the if statement with default final values as false or true. Just by changing the flag variable from false to true or vice-versa developers can recompile the program and use it differently.

Scenario 3:

public void helloWorld() {
   System.out.println("HelloWorld");
 while(true) {
      return;
  }
  System.out.println("World");
}

This Gives Unreachable Exception Compilation Error, as explained above.

于 2013-10-10T11:52:14.523 回答
2

You cannot have multiple returns in one method, this is why next row after return is unreachable

  public String seeDVDInfo() {
    return filmName;
    //return director; 
    //return leadActor;
}
于 2013-10-10T11:36:24.610 回答
0

Your're trying to return three Strings -> that fails.

Use this instead:

public List<String> seeDVDInfo() {
    List<String> dvdInfos = new ArrayList<>();
    dvdInfos.add(filmName);
    dvdInfos.add(director);
    dvdInfos.add(leadActor);
    return dvdInfos;
}
于 2013-10-10T11:38:07.707 回答