-2

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.
    }
}
Siavas
  • 4,992
  • 2
  • 23
  • 33
  • The error message is exactly what is says: your method should return `String`, but it returns nothing. –  Jan 21 '19 at 23:37
  • Replace `System.out.println(yourString)` to `return yourString` for all three cases. –  Jan 21 '19 at 23:39

4 Answers4

2

This is how you will return appropriate strings without the method reaching termination:

public static String milesFromMCTC(double miles){

    if (miles > 10) {
        return "You live more than 10 miles from MCTC";
    } else if (miles == 10){
        return "You live exactly 10 miles from MCTC";
    } else {
        return "You live less than 10 miles from MCTC";
    }

}

then print returned string from the calling function

AppleCiderGuy
  • 1,249
  • 1
  • 9
  • 16
  • After reviewing the previous comments I located my error which was writing the method for use with void. I reviewed separate code I had written for this in another Java file where I realized at this point I used void. I then tested your recommendation and it was functional. I will work to create modify it a bit so I can submit my homework. Thank you for your example. –  Jan 22 '19 at 00:17
2

Let's try to explain this in a bit more detail for the ones of us that are just starting...

In Java and many other strongly-typed languages, methods (such as milesFromMCTC(double miles) from your Question_1_Miles_From_MCTC class) need to have a specified return type, which for your method is String – the equivalent of a series of characters / text in programming jargon. This means that before ending the call to that method, a String must be returned inside it so that other programmers can guarantee they get the correct value and expected datatype when using your method. In your code, this is shown by the following statement: String response = milesFromMCTC(miles); in which you expect the return value of your method to be String. If nothing is returned by that method, what would the response variable store?

The approach that you have taken, despite being apparently valid for your homework, changes the direction in which your exercise was maybe taught – you are using System.out.println to print the result of your method call, but at the same time any string must be returned, therefore IntelliJ complains about the missing return statement. Consider that even null is acceptable for String in Java as it is not a primitive datatype as opposed to other languages.

On the other side, there is a special return type called void that will allow you to call methods and do things without having to return anything, such as in the following example:

public class HelloWorldClass {
    public static void main (String[] args) {
        // Here our method with void return type is called, which prints Hello
        sayHello();
    }

    private static void sayHello() {
        System.out.println("Hello!");
    }
}

Actually, if you look in your code, there is already a method that returns nothing (and thus void return type) – your main()!

So now that we know we either have to return String when our method return type is String, or return nothing when the return type is void, try to think how you could adapt your code to fix your problem and print the correct result. Then check if you were correct below :)

Solution as it was taught by your tutor:

// This is Java code so it will not run in StackOverflow snippet
public class Question_1_Miles_From_MCTC {

    public static void main(String[] args) {

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

        // get the correct answer as String (text) from your method and store it in response
        String response = milesFromMCTC(miles);

        // print the result here by passing response to System.out.println
        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) {
            return "You live more than 10 miles from MCTC";
        } else if (miles == 10){
            return "You live exactly 10 miles from MCTC";
        } else {
            return "You live less than 10 miles from MCTC";
        }
    }
}

Solution as per your approach, adapted to fix the missing return statement:

// This is Java code so it will not run in StackOverflow snippet
public class Question_1_Miles_From_MCTC {

    public static void main(String[] args) {

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

        // Now as our method is void we cannot store it in a variable, so we will let it print our answer
        milesFromMCTC(miles);

        // There are not applicable now
        // String response = milesFromMCTC(miles);
        // System.out.println(response);

    }

    // we have to change the return type here to void so that we don't have to return anything
    public static void 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");
        }
    }
}

As a final note, in a real programming scenario you would rather use the first solution, as it allows you to do anything you want with the answer, instead of being forced to print it to console which in most cases is only intended for debugging purposes.

Hope this helped understanding return types a bit. Good luck with the rest of your homework!

Siavas
  • 4,992
  • 2
  • 23
  • 33
0

If you delete the return null;' you won't be returning anything. Since you've declared the return type in your method as being of type String you need to return a String value, or null. As this is homework that should be enough to get you going.

Chris
  • 3,437
  • 6
  • 40
  • 73
  • Hi Chris, Thank you for your help. I overlooked the String as method. In the code I had made in a separate Java file I had used the Method Void which was my mistake. –  Jan 22 '19 at 00:14
0

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.

Casey Anderson
  • 452
  • 3
  • 9
  • Once I read your comment, I reviewed the code in the Java file I had made separate from the homework and I ended up using void. The homework calls for string. Thank you for helping me spot and review this. –  Jan 22 '19 at 00:16