Two concepts I realized I needed to understand and use more are recursion and Exceptions. Thus, I combined both in the following program. Although it began with a focus on using recursion it became more about Exceptions. I think the recursive part is bare-bones and straight-forward but I would nonetheless appreciate any tips/advice on using/formatting recursion in general.
On exception-handling: Is the following the optimal way of employing them or is there a more conventionally favored fashion? I get the feeling I may be missing doing something unnecessary or missing something important.
import javax.swing.JOptionPane;
class FactorialDemo {
public static void main(String[] args) {
int factorialNumber = 0;
try {
factorialNumber = Integer.parseInt(JOptionPane.showInputDialog(null,
"Which number should we compute the factorial of?"));
} catch(NumberFormatException nfe) {
JOptionPane.showMessageDialog(null,"Input must be an integer!",
"Error", JOptionPane.ERROR_MESSAGE);
throw new NumberFormatException("Input must be an integer.");
}
JOptionPane.showMessageDialog(null,
factorialNumber + " factorial is " +recur(factorialNumber) +".",
"Result", JOptionPane.PLAIN_MESSAGE);
}
public static int recur(int n) {
int result;
if (n < 0) {
JOptionPane.showMessageDialog(null,"n must be >= 0 but was " + n,
"Error", JOptionPane.ERROR_MESSAGE);
throw new IllegalArgumentException("n must be >= 0 but was " + n);
}
if (n < 2) {
return 1;
}
result = recur(n - 1) * n;
return result;
}
}