0

How to validate an integer value through form in java

if(!(Integer.parseInt(txtStart.getText()).equals(txtStart.getText()))
{
    JOptionPane.showMessageDialog(null, "Please Enter Valid Start Page");
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Saurav Dhital
  • 61
  • 1
  • 1
  • 6

1 Answers1

0

With the next code you can validate is your text is a number.

public class Main {

    public static void main(String args[]){
        String txtValue1 = "";
        String txtValue2 = "a";
        String txtValue3 = "3";

        System.out.println("txtValue1: "+isNumber(txtValue1));
        System.out.println("txtValue2: "+isNumber(txtValue2));
        System.out.println("txtValue3: "+isNumber(txtValue3));
    }

    private static boolean isNumber(String _value){
        boolean result = true;

        try{
            Double.parseDouble(_value); 
        }catch(Exception e){
            result = false;
        }
        return result;
    }

}

Output

txtValue1: false
txtValue2: false
txtValue3: true
Rendón
  • 300
  • 1
  • 7