# Thread: Static Method and Return Statements

1. Member
Join Date
Nov 2008
Location
Boston
Posts
2
Rep Power
0

## 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
Last edited by berelson; 11-29-2008 at 11:26 PM.

2. 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".

3. Member
Join Date
Nov 2008
Location
Boston
Posts
2
Rep Power
0
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);
Last edited by berelson; 11-29-2008 at 11:58 PM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•