Howdy Friends;
Here is part one of my two piece assignment for this week. I have posted the instructions and my attempt below. I believe that I have successfully completed sections 1 - 4. However; I am unsure how to do part five. I plan to post the second part of the total assignment after I complete this section and try the second part. Thank you for any help that you can provide.
Here is part one of my two piece assignment for this week. I have posted the instructions and my attempt below. I believe that I have successfully completed sections 1 - 4. However; I am unsure how to do part five. I plan to post the second part of the total assignment after I complete this section and try the second part. Thank you for any help that you can provide.
Quote
In this project you will implemented a factorial function.
1. You must add exception handling to your program
2. You will have to catch an InputMismatchException Exception and
3. you should make your own exception and throw it if a person enters a negative number (called NegNumberException). When you catch the NegNumberException then the program should thank the user for using there program and exit nicely. Hint: You will have to look at the throws reserved word.
4. The program description for the factorial program is: Write a program that asks the user for a number and outputs the factorial of that number. The factorial of a number n is n * (n-1) * (n-2) *...* 2 * 1.
5. The program should execute repeatedly, until the user enters a negative value. The program should throw an exception if the user enters a non-integer value.
1. You must add exception handling to your program
2. You will have to catch an InputMismatchException Exception and
3. you should make your own exception and throw it if a person enters a negative number (called NegNumberException). When you catch the NegNumberException then the program should thank the user for using there program and exit nicely. Hint: You will have to look at the throws reserved word.
4. The program description for the factorial program is: Write a program that asks the user for a number and outputs the factorial of that number. The factorial of a number n is n * (n-1) * (n-2) *...* 2 * 1.
5. The program should execute repeatedly, until the user enters a negative value. The program should throw an exception if the user enters a non-integer value.
/* * The program should execute repeatedly, until the user enters a negative value. */ package exceptionhandlerfactorial; import java.util.InputMismatchException; import java.util.Scanner; class ExceptionHandlerFactorial { private static Scanner console = new Scanner(System.in); private static int userInput = 0; private static boolean acceptableUserInput = false; public static void main(String[] args) { do{ System.out.println("Please enter initial value"); try{ userInput = console.nextInt(); if (userInput < 0){ throw new NegNumberException(); } else { System.out.println(getFactorial(userInput)); acceptableUserInput = true; } } catch(InputMismatchException e){ System.err.println("Caught InputMismatchExcpection," + " please enter a valid integer"); //clear buffer console.next(); //retry continue; } catch(NegNumberException e) { System.err.println("Caught NegNumberException," + " now ending the program"); break; } }while(!(acceptableUserInput)); } static class NegNumberException extends Exception { public NegNumberException() { } } static int getFactorial(int input){ int fact = 1; for(int x = input; x > 1; x--){ fact *= x; } return fact; } }