This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Can't get the result of factorial method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Can Watch "Can New topic
Author

Can't get the result of factorial method

Luqman Ghani
Greenhorn

Joined: Jun 30, 2011
Posts: 9
public class Factorial {
/** Compute and return x!, the factorial of x. */
public static int factorial(int x) {
int fact = 1;
for (int i = 2; i <= x; i++ ) {
fact *= i;
}
return fact;
}
}


I type "java Factorial 3" in the command window and it does not return 3! (or any other x!). Instead it gives an error:
"Static Error: No method in static Factorial has name 'main' "...

I copied it from a book which says that it isn't a program but a method itself.

What should I do so that the command window may return the factorial?


I wanted to be ideal,... but I came to know that it's just an idea!
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Hi,

Your class has no main() method which is the starting point of execution for a Java class. Can you add the main method and try calling the factorial() method from it.

Can you search in the book & find how to write a main method and call the factorial method you already have?

Welcome to the Ranch
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Hi Luqman,

If you have not found any... please try below.


Luqman Ghani
Greenhorn

Joined: Jun 30, 2011
Posts: 9
Thanks,...
Actually it said that the above method can be called in another class. Now I understand why every class has a main in it.

thanks a lot!!!

One more thing...
How do you write "God save the king" at the bottom of the post!!
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Welcome... Click "My Profile" link which you see at the top under the "The Big Moose Saloon" site poster. And give a signature under General Information about yourself tab in your profile edit page.

Debbie Waltz
Greenhorn

Joined: May 12, 2008
Posts: 13
Luqman Ghani wrote:Thanks,...
Now I understand why every class has a main in it.


actually at least one should have a main in it. The one you ask to the JVM to execute. The possible main() of other classes will be ignored.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can't get the result of factorial method
 
Similar Threads
Recursion
Hoare-Calculus
Exception in thread "main" java.
in.Reader
Returning an arrray from a method?