Search code examples
javaif-statementintellij-ideareturnreturn-type

Missing return statement error, after deletion of return statement


I am working on a Java Homework assignment. I have to use if, else if, else statements to output a statement.

The three possibilities that can be generated:

  • Example 1: user inputs 7, output is "you live less than 10 miles from MCTC".

  • Example 2: user inputs 11, output is "you live more than 10 miles from MCTC".

  • Example 3. user inputs 10, output is "you live exactly 10 miles from MCTC".

Here are 4 images showing my issue.

I have created my own version of this outside of the code for the assignment the instructor has provided, and I obtain the results that are desired.

When I delete the line: return null; // TODO delete this line and replace with your code., I receive error while hovering over 2nd to last curly brace: missing return statement

When I run the program I have posted an image that shows the error.

public class Question_1_Miles_From_MCTC {

    public static void main(String[] args) {

        double miles = doubleInput("How many miles do you live from MCTC? ");

        String response = milesFromMCTC(miles);

        System.out.println(response);

    }

    public static String milesFromMCTC(double miles){

        // TODO Use if - else if - else statements to return the correct String

        // Return  "You live more than 10 miles from MCTC" if they live more than 10 miles away,
        // Return  "You live exactly 10 miles from MCTC" if they live exactly 10 miles away,
        // Return  "You live less than 10 miles from MCTC" if they live less than 10 miles away.
        if (miles > 10) {
            System.out.println("You live more than 10 miles from MCTC");
        } else if (miles == 10){
            System.out.println("You live exactly 10 miles from MCTC");
        } else {
            System.out.println("You live less than 10 miles from MCTC");
        }

        return null; // TODO delete this line and replace with your code.
    }
}

Solution

  • You either need to return a string by assigning it to a variable or need to list 'void' instead of String in the function declaration.