4 Replies - 592 Views - Last Post: 29 November 2011 - 08:38 PM Rate Topic: -----

#1 Th3T3chGuy  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 73
  • Joined: 16-October 11

Missing return statement error

Posted 29 November 2011 - 08:22 PM

Here is my code:

import java.util.Scanner;
public class Problem7{
	public static void main (String [] args){
	Scanner reader = new Scanner(System.in);
		
	int num = 0;
	int product = 0;
	
	System.out.println("Enter an integer:");
	num = reader.nextInt();
			
	int factorial = computeFactorial(num, product);	
	}
		
public static int computeFactorial(int num, int product){
	
	int factorial = 0;
	for (int i = 1; i < num; i++){
	if (num % i == 0){
 		product += i; 
 	if (product == num){	
 	return factorial;
	}
	}
	}	
	}
}	


I get this error:

---------- Capture Output ----------
"C:\Program Files\Java\jdk1.7.0\bin\javac.exe" Problem7.java
Problem7.java:26: error: missing return statement
	}
	^
1 error
Terminated with exit code 1


Is This A Good Question/Topic? 0
  • +

Replies To: Missing return statement error

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10447
  • View blog
  • Posts: 38,690
  • Joined: 27-December 08

Re: Missing return statement error

Posted 29 November 2011 - 08:23 PM

If you properly indent your code, the error becomes apparent. Also, you aren't calculating a factorial correctly. Remember that n! = n * (n-1) * (n-2) * (n-3) ... 1.
Was This Post Helpful? 0
  • +
  • -

#3 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Missing return statement error

Posted 29 November 2011 - 08:25 PM

You only return if this condition is true:
if (product == num){   
   return factorial;
}

What if this condition is false, so no return type...
Was This Post Helpful? 1
  • +
  • -

#4 Th3T3chGuy  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 73
  • Joined: 16-October 11

Re: Missing return statement error

Posted 29 November 2011 - 08:37 PM

View Postmacosxnerd101, on 29 November 2011 - 08:23 PM, said:

If you properly indent your code, the error becomes apparent. Also, you aren't calculating a factorial correctly. Remember that n! = n * (n-1) * (n-2) * (n-3) ... 1.


Great. That made my code shorter and to the point. Easier than I thought.
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10447
  • View blog
  • Posts: 38,690
  • Joined: 27-December 08

Re: Missing return statement error

Posted 29 November 2011 - 08:38 PM

Glad I could help! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1