2

as part of the program iam writing there is to be an option for the user to enter a number to obtain the facotrial of. So far i have this:

number = Integer.parseInt(JOptionPane.showInputDialog("\nPlease enter the number you wish to obtain the factorial for"));
    JOptionPane.showMessageDialog(null, "You entered the number: " + number);


    count = (number - 1);

    if(number > 1)
    {

    do

    {

    factorial = number * count;
    JOptionPane.showMessageDialog(null, number + " * " + count + " = " + factorial);
    count--;
    number = factorial;

    }

    while(count > 0);

    JOptionPane.showMessageDialog(null, number + " * " + count + " = " + factorial);

    JOptionPane.showMessageDialog(null, "The facorial of " + number + " is " + factorial);

    }

    else if (number == 0)

    {

    JOptionPane.showMessageDialog(null, "Factorial of 0 is 1");

    }

    else if (number == 1)

    {

    JOptionPane.showMessageDialog(null, "Factorial of 1 is 1");

    }

    else if (number < 0)

    {

    JOptionPane.showMessageDialog(null, "Please enter a number equal to or greater than 0");

This works fine but the brief is to also include an error message prompting the user ro enter a whole number when a decmimal number has been used and also if a letter is inputted. I am struggling to write code to achieve this.

Thanks in advance

Ryan

rymo
  • 23
  • 3
  • Integers can't have decimal points. You have to use a Double. – Angelo Alvisi Jan 20 '15 at 14:08
  • The below topic is for you (note, that the accepted answer isn't the best because it catches exceptions even if nothing is wrong, so your best go is StringUtils.isNumeric() function. http://stackoverflow.com/questions/1102891/how-to-check-if-a-string-is-a-numeric-type-in-java – Attila Neparáczki Jan 20 '15 at 14:12

2 Answers2

0

You can use regular expressions to ensure the number is not a decimal

if (!"wrong_number".matches("^-?\\d+$")) {
    //error
}

As an addition to this, never assume your input is correct, always check first do conversions later :)

epoch
  • 16,396
  • 4
  • 43
  • 71
  • This seems needlessly complicated, he should just catch the `NumberFormatException` that would be caused by trying to parse a non-int into an integer. – user3062946 Jan 20 '15 at 14:12
  • a matter of opinion i guess. I like to validate, not recover. – epoch Jan 20 '15 at 14:13
0

The problem is that when the input is invalid, Integer.parseInt throws an exception, breaking out of the flow of your method. The trick to solving this is moving the call of Integer.parseInt inside the body of your code where you could deal with any exceptions resulting from invalid input. You can do it by storing the input into a String, and catching the exception when parsing it:

String input = JOptionPane.showInputDialog("\nPlease enter the number you wish to obtain the factorial for");
number = -1;
try {
    number = Integer.parseInt(input);
} catch (NumberFormatException nfe) {
    ... // Show a message here saying that "input" is invalid
}

Note the try / catch block. NumberFormatException is what Integer.parseInt throws when you pass it invalid input.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523