Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can't get the result of factorial method

 
Luqman Ghani
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Luqman,

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


 
Luqman Ghani
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Jack Dwaltz
Greenhorn
Posts: 17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic