Question

I'm using italian locale for my program so Float.parseFloat("8,00") must function well. But I have encountered a very bad NFE in the following line:

this.cuSurfaceJTextField1.setValue(
    String.format("%05.2f",info.getCuSurface()));

I note that the above code used to work well up to some changes I made to the listeners that don't look to be related to this line of the code.(Now I have a propertyChangeListener that updates the model when the value is changed.

this.cuSurfaceJTextField1.addPropertyChangeListener("value", 
        new PropertyChangeListener() {

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        info.setCuSurface(Float.parseFloat(
                (String)cuSurfaceJTextField1.getValue()));
        updateCuSurface();
    }
});

The useful part of the exception:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "08,00"
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1241)
    at java.lang.Float.parseFloat(Float.java:452)
    at View.bars.QuadrateJPanel$11.propertyChange(QuadrateJPanel.java:348)
    at java.beans.PropertyChangeSupport.fire(PropertyChangeSupport.java:335)
    at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:328)
    at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:263)
    at java.awt.Component.firePropertyChange(Component.java:8382)
    at javax.swing.JFormattedTextField.setValue(JFormattedTextField.java:799)
    at javax.swing.JFormattedTextField.setValue(JFormattedTextField.java:502)
Was it helpful?

Solution

I'm using italian locale for my program so Float.parseFloat("8,00") must function well.

No. Float.parseFloat is not locale dependent. So, there are two ways to fix your issue:

  • Format like this:

    String.format(Locale.US, "%05.2f",info.getCuSurface())
    
  • Replace the comma when you parse:

    info.setCuSurface(Float.parseFloat(
            ((String) cuSurfaceJTextField1.getValue()).replace(',','.')));
    

OTHER TIPS

Looks like there may be some confusion over your Locale? I've seen this myself - in America and other places numbers are written as "8.00" but in parts of Europe (France was what gave me trouble) it is written as "8,00". Seems like when String.format() is called it is in a Locale that uses commas... but setting the value on the JTextField is automatically parsing the passed-in string with Float.parseFloat() which is not sensitive to Locale matters - it looks for . and that's it.

parseFloat doesn't use the default locale, it calls FloatingDecimal.readJavaFormatString(s).floatValue().

Instead use NumberFormat which will honour the Locale set

public class NFE {

    public static void main(String[] args) throws Exception
    {
        Locale.setDefault(Locale.ITALIAN);
        NumberFormat format = NumberFormat.getInstance();
        //Float.parseFloat("8,00");
        System.out.println(format.parse("8,00"));

    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top