Srm
Srm

Reputation: 21

Java - Receiving a missing return error

I started taking a Java class last week and one of my assignments is to create a program that will take the GCD of two numbers, using what we have learned in class so far (so my program is probably not the most efficient). This is my program.

import java.util.*;
public class Program {
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        System.out.print("Enter an integer");
        int num1 = console.nextInt();
        System.out.print("Enter an integer");
        int num2 = console.nextInt();
        int gcd = factor(num1, num2);
        System.out.println("The GCD is " + gcd);
    }

    public static int factor(int num1, int num2) {
        int big = Math.max(num1, num2);
        int small = Math.min(num1, num2);
        int bigabs = Math.max(Math.abs(num1), Math.abs(num2));
        int smallabs = Math.min(Math.abs(num1), Math.abs(num2));

        if (smallabs == 0) {
            return bigabs;
        }
        if ((bigabs == 1) || (smallabs == 1)) {
            return 1;
        }
        for (int i = smallabs ; i >= 1; i--) {
            if ((num1 % i == 0) && (num2 % i == 0)) {
                return i;
            } 
        }
        // This is where I need a return statement
    }
}

It can't compile because it's missing a return statement. I believe that is because a method which requires a return type must declare a return statement for every possible branch in the method (correct me if I'm wrong). I know that in any scenario the numbers will go through the for loop's if statement, but the method doesn't know that. So my question is what return statement should I put? Does it really matter? Can someone explain this to me in detail because I have not learned much yet.

Upvotes: 1

Views: 102

Answers (1)

GhostCat
GhostCat

Reputation: 140633

I think the "lesson to learn" here isn't directly if you should return 1; or further rework your loop.

The key element here is what you wrote: I know that in any scenario the numbers will go through the for loop's if statement, but the method doesn't know that.

But the point is: your program is the expression of an algorithm. Each and any statement in that program must make "absolute" sense. Yes, sometimes programming languages are restricted; and you have to write something down just to make the compiler happy. But that is typically a bad sign. It means that you should probably spend some more time thinking about your code. In other words: any program should read "as natural" as possible. You only do things in your programs that make sense.

So, if you understand that your method must return from the loop statement at some point, and will never reach the end of the method, then a solution like

public int whatever() {
  ... for ( ... {
    return x;
  }

  throw new RuntimeException("will never be reached");

}

tells "everybody" what is going on: the compiler has a way to deal with the missing return; and human readers also understand the purpose of this construct easily.

Upvotes: 1

Related Questions