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!