NumberFormatException in String.Format ("% 05.2f", 8.00)

I use Italian for my program, so it Float.parseFloat("8,00")should function well. But I came across a very bad NFE in the following line:

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

I note that the code above was used to work well with some of the changes I made for listeners that don't seem to be related to this line of code. (Now I have aChangeListener property that updates the model when the value changes.

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

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

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)
+4
source share
3 answers

, Float.parseFloat("8,00") .

. Float.parseFloat . , :

  • :

    String.format(Locale.US, "%05.2f",info.getCuSurface())
    
  • :

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

, - Locale? - "8,00", ( , ), "8,00". , String.format() , Locale, ... JTextField Float.parseFloat(), Locale - . .

+1

parseFloat , FloatingDecimal.readJavaFormatString(s).floatValue().

NumberFormat, Locale

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"));

    }
}
+1

All Articles