Problem with Validating the Integer Value in Java

非 Y 不嫁゛ 提交于 2019-12-04 17:16:11

As you are using a listener, you can empty the text field, instead of making it not-editable. You can do something like this, the snippet is based upon your code.

txtCapacity.addKeyListener(new KeyAdapter() {
        public void keyReleased(KeyEvent EVT) {                     
             if(!(EVT.character>='0' && EVT.character<='9')){
                    txtCapabity.setText(""); 
             }
        }
});

Or better if you use JFormattedTextField. I'm not sure if you have that in SWT, even if you don't then try to look for the similar.

Dont use the KeyListener! Use a VerifyListener instead as this will handle paste, backspace, replace.....

E.g.

text.addVerifyListener(new VerifyListener() {
  @Override
  public void verifyText(VerifyEvent e) {
    final String oldS = text.getText();
    final String newS = oldS.substring(0, e.start) + e.text + oldS.substring(e.end);

    try {
      new BigDecimal(newS);
      // value is decimal
    } catch (final NumberFormatException numberFormatException) {
      // value is not decimal
      e.doit = false;
    }
  }
});

Another possibility is to use the FormattedTextField-Widget from Nebular, see http://www.eclipse.org/nebula/widgets/formattedtext/formattedtext.php The advantage is that you only have to provide a pattern, there is no need to write your own listeners..

You can use this function to validate if the number:

    public static int validateInteger(String number)
    {
        int i = -1;

        try {
            i = Integer.parseInt(number);
        }
        catch (NumberFormatException nfe)
        {}
        catch (NullPointerException npe)
        {}

        return i;
    }

If the function returns a value less than zero, then it is not a valid positive number.

For validating if a value is infact decimal or not, you can simply use -

try {
        new BigDecimal(value.toString());
        // value is decimal
} catch (NumberFormatException numberFormatException) {
    // value is not decimal
}

You should probably set a Document on your text box. You can implement a customer Document to filter for valid input to satisfy your requirements. Each time text is added or removed from your field, the Document will check if the overall input is valid.

Chetan Solanki
txtfield.addKeyListener(new KeyAdapter() {
    public void keyPressed(KeyEvent evt) {                     

        char c=evt.getKeyChar();
        if(Character.isLetter(c))
        {
            JOptionPane.showMessageDialog(null, "PLEASE ENTER A DIGIT", "INVALID NUMBER", JOptionPane.ERROR_MESSAGE);
            txtfield.setBackground(Color.PINK);
            txtfield.setText("");

            String s = txtfield.getText();
            if(s.length() == 0){
                txtfield.setBackground(Color.PINK);
            }
        }
        else
        {
            txtfield.setBackground(Color.white);
        }            
    }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!