This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Hands On Software Engineering with Python and have Brian Allbey on-line!
See this thread for details.
Win a copy of Hands On Software Engineering with Python this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Can't get the result of factorial method  RSS feed

 
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?
 
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.

 
Greenhorn
Posts: 18
  • 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.
 
Trust God, but always tether your camel... to this tiny ad.
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!