-1

I wanted to validate the TextField of GUI, If user cannot enter string in size Text field that only take integers as an argument. Like catching Exceptions enter image description here

Here is my code Its not catching the exception

public boolean validateTextField(){
    if(eventSizeField.getText().isEmpty() || eventNameField.getText().isEmpty()){
        alertboxCustom("Empty Field", "Please Enter Valid Name and size");
        return false;
    }else if(Integer.parseInt(eventSizeField.getText()) <= 0 ){
        alertboxCustom("EventSize", "Event Size must be greater than zero");
        return false;
    }try{
         Integer.parseInt(eventSizeField.getText());
    }catch (NumberFormatException e) {
        alertboxCustom("EventSize", e.getMessage());
        System.out.println(e.getMessage());
        return false;
    }
    return true;
}
Reboot
  • 63
  • 10
  • Every input from TextField is interpreted as String, you want to check if it's for example a number? – Michael Dz May 25 '17 at 15:55
  • I know every input is a string, Name of the event take the string and Size of the event only take integers as an input to create an object. I want to display error alert box if someone try to enter string in that field. – Reboot May 25 '17 at 16:00
  • Please [edit] the question to make it clear what you are asking. Do you want the text field to only be able to have numerical (integer? floating point? negative?) values? If so, explain that in the question in a way that is understandable. – James_D May 25 '17 at 16:13
  • @Reboot `if(Integer.parseInt(eventSizeField.getText()) <= 0 )` condition has to be after try catch otherwise try block makes no sense. – Michael Dz May 25 '17 at 16:14
  • I already put my code in edited version – Reboot May 25 '17 at 16:14
  • It worked, Thanks! – Reboot May 25 '17 at 16:19
  • 1
    If you’re asking how to make a TextField accept only numbers, you probably want `eventSizeField.setTextFormatter(new TextFormatter(new NumberStringConverter(NumberFormat.getIntegerInstance())));`. – VGR May 25 '17 at 16:20
  • @VGR you need a filter there, as well as the converter. – James_D May 25 '17 at 16:24
  • you can use some thing like this https://stackoverflow.com/a/30796829/6296931 – Coder ACJHP May 25 '17 at 16:58

2 Answers2

1

You can use try catch for this:

    try {
            Integer.parseInt(textField.getText());
            //Some operations
        } catch (NumberFormatException e) {
            //Here for example prompt some window notifying user
            System.out.println(e);
        }
Michael Dz
  • 3,655
  • 8
  • 40
  • 74
0

In java every text is string. get the string from text field

String stringValue=textField.getText(); 

To get integer value from string we can parse it using

  try{
    value=Integer.parseInt(stringValue)
    } catch (NumberFormatException e) {
                //prompt some error or alert
    //reset text
      textField.setText("");
  }
Binu
  • 754
  • 6
  • 15
  • Never use try-catch for logical processing. Exceptions are for exceptional conditions. You should write code to avoid converting the string to a number if it is not the correct format. – James_D May 25 '17 at 16:11
  • And also It will slow the speed – Coder ACJHP May 25 '17 at 17:02