1

So i was making an application where the user enters an input, and when he clicks the button it executes a command, however the input has to be an integer, so i added a check but even when i enter an integer is gives me an error saying "you can enter numbers only!"

heres my code :

        String itemId = textField1.getText();
        String itemAmount = textField2.getText();

        int id = Integer.parseInt(itemId);
        int amount = Integer.parseInt(itemAmount);

        if (!Double.isNaN(id) || !Double.isNaN(amount)){
            JOptionPane.showMessageDialog(
                    null, "You can only enter numbers!"
            );

even after i enter numbers to the textFields i still cannot pass this test, why and how can i fix this ? thanks.

Boolena
  • 225
  • 1
  • 2
  • 10
  • possible duplicate of [Restricting JTextField input to Integers](http://stackoverflow.com/questions/11093326/restricting-jtextfield-input-to-integers) – alex2410 Mar 25 '14 at 09:19
  • That question asked a lot of times here, try to search, before ask that again. – alex2410 Mar 25 '14 at 09:25

4 Answers4

5

however the input has to be an integer, so i added a check but even when i enter an integer is gives me an error saying "you can enter numbers only!"

there are two ways, to use

  • JFormattedTextField with number formatter, JSpinner with SpinnerNumberModel

  • add DocumentFilter to JTextField

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 3
    *"`JSpinner` with `SpinnerNumberModel`"* Happiness. :) This best (IMO) approach is the way to go. – Andrew Thompson Mar 25 '14 at 12:25
  • +1 Totally agree. If people have worked hard to give us the solution to the input-validation problem through such components (`JSpinner` and `JFormattedTextField`), why reinvent the wheel? – dic19 Mar 25 '14 at 14:26
2

Actually you can just do it like this:

String itemId = textField1.getText();
String itemAmount = textField2.getText();
int id;
int amount;
try{
    id = Integer.parseInt(itemId);
    amount = Integer.parseInt(itemAmount);
}
catch(NumberFormatException e){
        JOptionPane.showMessageDialog(null, "You can only enter numbers!");
}

If itemID and itemAmount value is not parse-able, means non-digit was entered

Baby
  • 5,062
  • 3
  • 30
  • 52
  • 1
    Catch `NumberFormatException`, not `Exception`. Otherwise, if something else is broken, and you get a `NullPointerException` or an `ArrayIndexOutOfBoundsException`, it will still say "You can only enter numbers!" when the problem is nothing to do with entering numbers or not. – user253751 Mar 25 '14 at 09:20
  • 1
    doesn't matter. `Exception` will still catch the `NumberFormatException`. But okay, i'll modify it.(I don't think `NullPointerException` or an `ArrayIndexOutOfBoundsException` can happen inside that 2 lines try block :)) – Baby Mar 25 '14 at 09:22
  • 1
    @TAsk *"The Simplest One!!"* Way no! You should check out `JSpinner` with `SpinnerNumberModel`for this, as mentioned in [another answer](http://stackoverflow.com/a/22629861/418556). Much simpler than trying to force a square peg (integer) into a round hole (text field). :) For [example](http://stackoverflow.com/a/10021773/418556).. – Andrew Thompson Mar 25 '14 at 12:31
  • 1
    @TAsk .. or [another example](http://stackoverflow.com/a/6111095/418556) or [this slightly more complicated example](http://stackoverflow.com/a/17874718/418556) (which uses 4 spinners to trigger a `ChangeListener` to update the GUI).. – Andrew Thompson Mar 25 '14 at 12:41
1
 Double.isNaN(id)

Returns true if the specified number is a Not-a-Number (NaN) value, false otherwise.

But your id and your amout are Integers so it will return false and you do !Double.isNaN(id) and invert the boolean, so the result is true. Its only an logical failure. Remove the !.

if (Double.isNaN(id) || Double.isNaN(amount)){
    JOptionPane.showMessageDialog(null, "You can only enter numbers!");
}

Note:

int id = Integer.parseInt(itemId);
int amount = Integer.parseInt(itemAmount);

Sourround this two lines with an try and catch block, otherwise you will get an NumberFormatException if the input is not numeric.

try
{
    int id = Integer.parseInt(itemId);
    int amount = Integer.parseInt(itemAmount);+
}catch(NumberFormatException e)
{
    //print your error here
}
kai
  • 6,702
  • 22
  • 38
0

Use Apache's StringUtils.isNumeric() method, if you don't want to use JFormattedTextField

Sorter
  • 9,704
  • 6
  • 64
  • 74