Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## Factorial Test Program

This is a homework assignment that have been unable to complete. I've been working on it for 6 days with no success. Everything I try causes the code to fail.

The code I'm working on is a test program to run against this code.

```// method factorial
// computes the factorial for 01 to 12! using a table lookup
// factorials larger than 12! exceed the largest value that can be stored
// in a Java primitive integer
public class Factorial
{
public static int factorial(int n)
{
final int f[] = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 479001600};
return f[n];
}
}```

The test program needs to catch any exceptions that occur so the program doesn't terminate. The number range is factorials for 0 through 12 only. If the user enters a negative number, a number higher than 12, blank, special characters, or alphabet the exception should be handled.

```import java.util.InputMismatchException;
import java.util.Scanner;

public class FactorialTest
{
public static void main (String args[])
{
Factorial f = new Factorial();

// create scanner object to read user input
Scanner scanner = new Scanner(System.in);

// prompt user to enter a number between 0 and 12
System.out.println("Please enter a number between 0 and 12");

// store value entered by user
int n = scanner.nextInt();

// assert the value of n is between 0 and 12
assert (n >= 0 && n <= 12);

System.out.println("Factorial of "+n+" is = "+f+"");
try
{
}
catch (InputMismatchException inputMismatchException)
{
System.err.printf("/nException: %s\n", inputMismatchException);
scanner.nextLine(); // discard input so user can try again
System.out.println("You must enter a number between 0 and 12");
}

if (n < 0)
System.out.println("Number should be non-negative.");
else
{
for (n = 0; n <= 12; n++);

}
}
}```

I checked with my college campus but they do not have any tutors to help with Java programming. I also contacted my instructor but he is not available on weekends. This assignment is due on 2/23/2014.

The final output needs to look something like this:

Enter an integer number: abc
You must enter an integer - please re-enter:
Enter an integer number: -5
Factorial of this value cannot be represented as an integer.
Enter an integer number: 13
Factorial of this value cannot be represented as an integer.
Enter an integer number: 9
The factorial of 9 is 362880.
Done!

Output when a letter is entered
Please enter a number between 0 and 12
a
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at FactorialTest.main(FactorialTest.java:25)

Output when a number between 0 and 12 is entered
Please enter a number between 0 and 12
8
Factorial of 8 is = Factorial@171bbc9

Output when a negative number is entered
Factorial of -9 is = Factorial@6f7ce9
Number should be non-negative.

The program is not working the way I need it to. When a number between 0 and 12 is entered the factorial amount should be pulled from main program and display in the output but it isn't
When a negative number is entered the output should only display "Number should be non-negative" and allow the user to enter another number.
When a letter is entered the output should display " Please enter a number between 0 and 12" and allow the user to enter another number.

Any assistance you can provide on what I'm doing wrong is greatly appreciated.

phendrickson

2. ## Re: Factorial Test Program

What happens when you compile and execute the posted code?

[code=java]
[/code]
to get highlighting and preserve formatting.

3. ## The Following User Says Thank You to Norm For This Useful Post:

phendrickson (February 23rd, 2014)

4. ## Re: Factorial Test Program

Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for newcomers.

After you fix your post so that your code is posted correctly (read the link above, then use the Edit Post button), please describe specifically what you need help with and/or ask specific questions. If you're getting errors, post those. If the output is not what is required, post a sample run and describe what needs to be changed.

5. ## The Following User Says Thank You to GregBrannon For This Useful Post:

phendrickson (February 23rd, 2014)

6. ## Re: Factorial Test Program

Study the try-catch block and the exceptions thrown by primitives e.g. int and arrays, so that you can tackle this assignment head on. You're pretty close to getting it, the try-catch block is all that remains.

7. ## The Following User Says Thank You to Mugambbo For This Useful Post:

phendrickson (February 23rd, 2014)

8. ## Re: Factorial Test Program

Thank you for your help, I added the try-catch but the program is not working the way I need it to.

9. ## Re: Factorial Test Program

program is not working the way I need it to.
Can you explain?
What does the code do?
What do you want it to do?

10. ## The Following User Says Thank You to Norm For This Useful Post:

phendrickson (February 23rd, 2014)

11. ## Re: Factorial Test Program

The final output needs to look like this:

Enter an integer number: abc
You must enter an integer - please re-enter:
Enter an integer number: -5
Factorial of this value cannot be represented as an integer.
Enter an integer number: 13
Factorial of this value cannot be represented as an integer.
Enter an integer number: 9
The factorial of 9 is 362880.
Done!

When a letter is entered the output should display " Please enter a number between 0 and 12" and allow the user to enter another number.
Instead this is the output I get

Please enter a number between 0 and 12
a
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at FactorialTest.main(FactorialTest.java:25)

When a number between 0 and 12 is entered the factorial amount should be pulled from the main program and display in the output but it isn't, instead the is what my output looks like.

Please enter a number between 0 and 12
8
Factorial of 8 is = Factorial@171bbc9

When a negative number is entered the output should only display "Number should be non-negative" and allow the user to enter another number.
Instead, this is the output I'm getting.

Factorial of -9 is = Factorial@6f7ce9
Number should be non-negative.

12. ## Re: Factorial Test Program

What do you want the program to do when the user enters bad data? Normally a program will print an error message and repeat the request to the user to enter the data correctly.
When using the nextInt() method the code should have it inside of a try{}catch block that catches the exception that is thrown and prints an error message. That whole thing must be inside of a loop (while) that continues looping until the user enters good data.

Factorial@171bbc9
That is the String returned by the Object class's toString() method when referencing the Factorial class. What did you expect to be printed when the code prints f? f is a reference to a Factorial class instance.
To get a value from the Factorial class, you need to call its method.

13. ## The Following User Says Thank You to Norm For This Useful Post:

phendrickson (February 23rd, 2014)

14. ## Re: Factorial Test Program

@Phendrickson, if you had caught the exception (InputMismatchException) in a try catch block, it wouldn't have displayed this exception, but rather, any string of your choosing.

15. ## The Following User Says Thank You to Mugambbo For This Useful Post:

phendrickson (February 23rd, 2014)