0

I am real beginner in Java and I have one simple exercise where I need to convert m/h into km/h using a method and a return from it.

I have to define 2 situations: if km/h < 0 return -1 (error) and if km/h > 0 return km/h * 1.609 (value in m/h).

I tried everything I could think of but I either get a no return statement error or no output when I try to run it.

I can't understand why even if I gave it more than one return option it just doesn't work whatever the value is. I could use System.outprintln or String but the exercise specify I must use a return method.

here is my code, written in IntelliJ:

package EXERCISE;

public class Main {

    public static void main(String[] args) {
        toMilesPerHour(0);
    }

    public static double toMilesPerHour(double kilometersPerHour) {

        if (kilometersPerHour < 0) {
            return -1;
        }
        else if (kilometersPerHour > 0) {
            return kilometersPerHour * 1.609d;
        }
        else if (kilometersPerHour == 0) {
            return 0;
        }

        return kilometersPerHour * 1.609;

        // if I don't write return here it gives me no return statement error,
        // if I write it, it gives me no output with value > or < 0 but no error.
    }

}
2
  • 2
    ? You don’t do anything with the returned value. Put a println in the main method with the returned value. Commented Feb 6, 2020 at 18:17
  • As Nathan says. Type System.out.println(toMilesPerHour(0)); in you main. That will display the returned double value. Commented Feb 6, 2020 at 18:23

5 Answers 5

1
public static double toMilesPerHour(double kilometersPerHour) {

    if (kilometersPerHour < 0) {
        return -1;
    }
    else {
        return kilometersPerHour * 1.609;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

print kilometerperhour.. Im interested what the input is
1

Try it like this:

public static double toMilesPerHour(double kilometersPerHour) {
    return (kilometersPerHour > 0 ? kilometersPerHour*1.609 : -1; 
}

You could also throw an exception if speed is negative:

public static double toMilesPerHour(double kilometersPerHour) {
    if (kilometersPerHour < 0) throw new IllegalArgumentException("speed cannot be negative");
    return kilometersPerHour*1.609; 
}

1 Comment

Doesn’t out put anything
0

Even if you use a method, you have to print the returned value:

package EXERCISE;

public class Main {

    public static void main(String[] args) {

        System.out.println(toMilesPerHour(0));

    }

    public static double toMilesPerHour(double kilometersPerHour) {

        if (kilometersPerHour < 0) {
            return -1;
        }
        else if (kilometersPerHour > 0) {
            return kilometersPerHour * 1.609d;
        }
        else if (kilometersPerHour == 0) {
            return 0;
        }
        return kilometersPerHour * 1.609;
        //if I don't write return here it gives me no return statement error,
        //if I write it, it gives me no output with value > or < 0 but no error.
    }
}

Furthermore, you can get rid of the return statement at the end:

    public static double toMilesPerHour(double kilometersPerHour) {
        if (kilometersPerHour < 0) {
            return -1;
        }
        else {
            // you don't need to check if kilometersPerHour is 0, since every number multiplied with 0 is 0
            return kilometersPerHour * 1.609;
        }
    }

2 Comments

I thought system.outprintln was optional. So when I just use return it only saves the value in memory right?
You could save it in memory by assigning the returned value to a variable: int mph = toMilesPerHour(0);. If you just have the statement toMilesPerHour(0), nothing is done with the returned value, but it is discarded.
0

If you use if()... else you have exactly two options. Either you go into the if clause and do whats there, or you go into the else clause. If if-clause and else-clause both have a return-statement, everything goes fine.

But you don't have if-else-clauses! You have if-else-if-clauses! If the if-clause doesn't hold, you go into the else-clause, which again has an if-clause and so on. Finally you don't have a default else clause.... if you don't get into the first if-clause, you check in the else-clause, if the condition holds. If not, you check the condition in the next else clause. The last return-statement finally is the default else clause. We all, as humans, see, that every condition (<, == >) is covered, but the compiler doesn't see that!

Comments

0

The reason compiler gives you "no return statement" error is because you didn't cover all possible cases with your ifs: there is Double.NaN, which is not equal to 0 (or any other value, for that matter, including itself) and neither greater nor lesser than 0.

Also, compiler doesn't analyze your code deep enough to check if you covered all possible variants anyway: if you replace double with long, the result is the same - the compiler will see the reachable branch that doesn't return anything, and so it produces an error. To fix an error, you need to have all branches return something:

if (kilometersPerHour < 0) {
    return -1;
}
else if (kilometersPerHour == 0) {
    return 0;
}

// Note: I don't have explicit check for NaN,
// because any arithmetic on NaN produces NaN,
// which would be the correct result for this function.
// So I let the "*" operator to check for NaN instead.
return kilometersPerHour * 1.609;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.