programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# MOOC Factorial help

Mike Skyriach
Greenhorn
Posts: 9
Hello guys.  I want to avoid using the internet to look up for a solution to solve a factorial by typing in a number after being asked for it, then have it calculate from 1*2*3 = 6 if I ask it to return a factorial for the number 3.   The calculation is supposed to come out as 6 for n=3.  However, for my code, the answer comes out as 4, not 6 for when n=3.  Can anyone give me a tip or help me think other way to get this done right without giving me an answer, please.  Appreciate it!

Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37465
539
• 1
Mike,
You are using i to mean two different things. Try to work out the factorial on paper. What information do you need to keep track of? Picture it as two columns.

Mike Skyriach
Greenhorn
Posts: 9
Jeanne Boyarsky wrote:Mike,
You are using i to mean two different things. Try to work out the factorial on paper. What information do you need to keep track of? Picture it as two columns.

Thanks for the quick reply!  I see..

I assume I have to declare another int variable.
1x2 = 2
2 x 3 = 6.

So I suppose I need to assign the next loop of the previous answer and multiply by the next incremental value.

Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37465
539
Mike Skyriach wrote:I assume I have to declare another int variable.
1x2 = 2
2 x 3 = 6.

So I suppose I need to assign the next loop of the previous answer and multiply by the next incremental value.

Yup!

Campbell Ritchie
Marshal
Posts: 56534
172
And don't use Integer.parseInt when the Scanner object can do the parsing for you.

Mike Skyriach
Greenhorn
Posts: 9
Campbell Ritchie wrote:And don't use Integer.parseInt when the Scanner object can do the parsing for you.

Really?  Why is the mooc.fi course teaching me to use Integer.parseInt and not Scanner parse method?  What's the difference between those two methods?

Campbell Ritchie
Marshal
Posts: 56534
172
Mike Skyriach wrote:. . . Why is the mooc.fi course teaching me to use Integer.parseInt and not Scanner parse method?
Good question. It is for them to justify why they are teaching that.
What's the difference between those two methods?
It isn't two methods; it is three. Integer.parseInt takes a String already provided, and Scanner.nextInt takes a file or the keyboard or similar and reads directly to an int. Note the names of the methods.You need some way to get the String into your program before you can parse it, so you need a preceding method call to get the text to parse.
The parseInt and nextInt methods are related to each other, but they throw different exceptions if your format is wrong. You can catch such exceptions, but you can avoid all Exceptions from the keyboard by combining nextInt and hasNextInt, as Rob Spoor taught me a long time ago.Lines 3 and 4 may go in either order.
That code will not throw exceptions as long as System.in remains open. It will not work reliably for reading files.

 With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.