This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!