# Static Method and Return Statements

• 11-29-2008, 11:03 PM
berelson
Static Method and Return Statements
Having basic problem with a program that is supposed to call static methods from main as part of homework assignment.

1 compile error: >javac Harmonic.java
Harmonic.java:24: missing return statement

on line: return Hlarge(N);

Seems to be having trouble with my if else or how i'm handling the returns of these static methods.

Thanks in advance for any help. Perhaps an easy fix for an intermediate developer.

--------------------------------------
public class Harmonic {
public static double Hsmall(int N) {
// Base case: H(1) = 1.
if(N <= 1) return 1;
// Every other case: H(n) = 1/n+H(n-1).
return Hsmall(N-1)+1.0/N;
}

public static double Hlarge(int N) {
return Math.exp(N); //library for Euler's constant
}

public static double H(int N) {
if (N < 100)
return Hsmall(N);
else if (N >= 100)
return Hlarge(N); // <-- compile error here
}

public static void main (String[] args){
//int N = Double.parseDouble(args[0]); //evaluate number
int N = Integer.parseInt(args[0]);
System.out.println(H(N));
}// main

}//class
• 11-30-2008, 12:03 AM
Fubarable
Your problem may be coming from the if / else if blocks (next time, please indicate with a code comment which line is causing the error). In this situation, the compiler doesn't know if there are more possibilities that you haven't accounted for,... if the if block is false and the else if is false, then what? Even if your else if covers all responsibilities, the compiler is not smart enough to figure this out. In that situation, you shouldn't be using else if, but should instead be using only "else".
• 11-30-2008, 12:17 AM
berelson
Thank you so much. That did the trick. I tried everything but your comments were pithy and very helfpul.

Solution :
if (N < 100)
return Hsmall(N);
else;
return Hlarge(N);