0

Need a java code that will sort integers in order.

This is a portion of my code but when I try to compile it, it says that it is missing a return statement. I am confused about how to fix this.

 public double getSmallest()
   {

      if (num1 <= num2 && num1 <= num3)

          return num1;


      if (num2 <= num1 && num2 <= num1)

          return num2;


      if (num3 <= num1 && num3 <= num2)

          return num3;


    }
1
  • 1
    what happens if none of the conditions are true? Therein lies the answer. Commented Nov 14, 2013 at 2:21

4 Answers 4

1

You have to return a value in the case that none of these conditions are true.

Either return a meaningless value like Double.MIN_VALUE (at least, meaningless in 99.9% of the cases) or throw an exception (a lot better!): throw new IllegalArgumentException().

Or, probably better: just refactor your conditions to make them automatically default to one value since that is really how this method should work. Like a waterfall.

Sign up to request clarification or add additional context in comments.

Comments

0

Since you are not using an else if chain but three separated ifs according to the compiler what can happen is that if c1 and c2 and c3 are all false

if (c1)
  return ..; << not executed
if (c2)
  return ..; << not executed
if (c3)
  return ..; << not executed
<< code flow arrives here but method ends: no return value

First of all, since these are mutually exclusive situations, you could use an else if:

if (c1)
  return ..;
else if (c2)
  return ..;
else if (c3)
  return ..;
else
  return 0; // will never be called

But since last case will never be chosen, because at least one condition will be true you can simply go:

if (c1)
  return ..;
else if (c2)
  return ..;
else
  return ..;

Comments

0

Regarding message it is missing a return statement, it is because in the following situation:

what it none of the 3 if condition is satisfied.

Have a try with the following code:

    public double getSmallest() {

       return num1 < num2 ? (num1 < num3 ? num1 : num3) : (num2 < num3 ? num2
            : num3);
    }

or

    public double getSmallest() {

       if (num1 < num2) {
           return num1 < num3 ? num1 : num3;
       } else {
           return num2 < num3 ? num2 : num3;
       }
    }

Comments

0

The problem is the compiler only sees return statements within if statements. It thinks there's no guaranteed path through the method that has a return.

Something like this would work:

if(num1 <= num2 && num1 <= num3) { 
  return num1;
} else if(num2 <= num1 && num2 <= num1) {
  return num2;
} else { //by matter of deduction
  return num3;
}

Another possible solution:

public double getSmallest() {
    double min = num1;
    if(min > num2) {
        min = num2;
    }
    if(min > num3) {
        min = num3;
    }
    return min;
}

As a note, this particular solution is easily the most scalable. If instead of a handful of numbers, you had an array of numbers, you could assign min to the value of the first index, and then iterate through the entire collection comparing the value at each index to min, and if it's smaller, reassign min.


Above are probably the two best and most logical solutions. However, simply adding return 0 (or ANY return statement) after all the if statements will make the compiler happy. This will work... but it's sloppy in my opinion (at least in this case).

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.