Search code examples
javanetbeans-8

Check textfield if it has multiple points


I want to create an error message if the user has entered multiple decimal point in the textfield.I can only check it if it has 1. but I dont know how if there are many.


Solution

  • Its not exactly what the question asks but would surely handle multiple dots and much more. The idea is to fetch the input as String from Textfield and then convert that string to double. If it throws an exception prompt user that the given input is invalid and ask to enter the value again. This would handle all your input data validation. Code would be something like this,

    String inputNum = myTextField.getText();
    double actualNum = 0.0;
    try {
      actualNum = Double.parseDouble(inputNum);  
    } catch (NumberFormatException ne){
        // Prompt User
    }