Search code examples
javaswingexceptionjtextfieldnumberformatexception

Couldn't Convert jTextfield string into float value..... Giving number format exception


Im experiencing a problem in parsing jTextfield into float value so i can perform operation but its giving number format exception

Here is the Error

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1011)
    at java.lang.Float.parseFloat(Float.java:452) 

and this is what im doing

float T1 = Float.parseFloat(txt_T1_f.getText());

Solution

  • You can add a check on the text value like this:

    String text = txt_T1_f.getText();
    if (text != null && !text.isEmpty()) {
        float T1 = Float.parseFloat(text);
    }