NumberFormatException in GUI

0

I have a program that simulates a pizzeria system where product prices are stored in enums as a double. The% window% takes these price values and displays it on the screen in this way: JLabel . Where "R$ "+ p.getValue; is the price. I got this String and put it inside a double variable (using p.getValue ). I make the sum of these variables and "game" into the text of a .substring(3); , until everything has been normal, but when I apply a JTextField to show two decimal places ( DecimalFormat , and execute, it applies this pattern, only in the console appears "NumberFormatException: For input String:" 37.49 ".

Method where I get the price ( df.applyPattern(##.##)) ) of the enum (double) item, as I change item in .getValue :

form.cmbPizza.addItemListener(new ItemListener() {  
            @Override
            public void itemStateChanged(ItemEvent e) {
                if(e.getStateChange() == ItemEvent.SELECTED){
                    Pizzas p = (Pizzas) e.getItem();
                    form.lblPrecoPizza.setText("R$ "+p.getValue());
                }else{
                    form.lblPrecoPizza.setText("R$ 0.00");
                }
            }
        });

Method that performs the formatting and conversion:

form.btnCalcular.addActionListener(new ActionListener() { //Apresentar soma dos produtos

        @Override
        public void actionPerformed(ActionEvent arg0) {
            form.precoPizza = Double.parseDouble(form.lblPrecoPizza.getText().substring(3));
            form.precoSuco = Double.parseDouble(form.lblPrecoSuco.getText().substring(3));
            form.precoRefri = Double.parseDouble(form.lblPrecoRefri.getText().substring(3));

            df.applyPattern("##.##");
            form.txtPrecoTotal.setText(String.valueOf(df.format(form.precoPizza + form.precoSuco + form.precoRefri)));
            double x = Double.parseDouble(form.txtPrecoTotal.getText()); //Linha onde é acusado o erro
            df.format(x);
            if(form.chkVip.isSelected()){
                double desconto = (form.precoPizza + form.precoRefri + form.precoSuco)*0.15;
                form.txtPrecoTotal.setText(df.format(String.valueOf(form.precoPizza + form.precoSuco + form.precoRefri - desconto)));
            }

        }
    });

Making it clear that this is a runtime error, not a compilation error, and that the error does not only occur with this value, but with any value!

    
asked by anonymous 06.04.2016 / 21:58

1 answer

1

Daniel, to treat values, instead of using R $ hardcode , you can use the class NumberFormat ", using a utility method like:

// Adicione esses atributos à sua classe
private Locale localeBrasil = new Locale("pt", "BR");
private NumberFormat formatoMonetario = NumberFormat.getCurrencyInstance(localeBrasil);

// Método utilitário
public double parseValor(String str) {
    Number valor = 0;

    try {
        valor = formatoMonetario.parse(str);
    } catch (ParseException e) {
    }

    return valor.doubleValue();
}

Replacing where you retrieve the value:

form.precoPizza = Double.parseDouble(form.lblPrecoPizza.getText().substring(3));

By:

form.precoPizza = parseValor(form.lblPrecoPizza.getText());

And where you arrow the value:

form.lblPrecoPizza.setText("R$ "+ p.getValue());

by:

form.lblPrecoPizza.setText(formatoMonetario.format(p.getValue()));

Getting something like this:

form.cmbPizza.addItemListener(new ItemListener() {  
            @Override
            public void itemStateChanged(ItemEvent e) {
                if(e.getStateChange() == ItemEvent.SELECTED){
                    Pizzas p = (Pizzas) e.getItem();
                    form.lblPrecoPizza.setText(formatoMonetario.format(p.getValue()));
                }else{
                    form.lblPrecoPizza.setText(formatoMonetario.format(0));
                }
            }
        });


form.btnCalcular.addActionListener(new ActionListener() { //Apresentar soma dos produtos

        @Override
        public void actionPerformed(ActionEvent arg0) {
            form.precoPizza = parseValor(form.lblPrecoPizza.getText());
            form.precoSuco = parseValor(form.lblPrecoSuco.getText());
            form.precoRefri = parseValor(form.lblPrecoRefri.getText());

            form.txtPrecoTotal.setText(formatoMonetario.format(form.precoPizza + form.precoSuco + form.precoRefri));
            double x = parseValor(form.txtPrecoTotal.getText());

            if(form.chkVip.isSelected()){
                double desconto = (form.precoPizza + form.precoRefri + form.precoSuco)*0.15;
                form.txtPrecoTotal.setText(formatoMonetario.format(form.precoPizza + form.precoSuco + form.precoRefri - desconto));
            }
        }
    });

Abcs!

    
07.04.2016 / 20:36