Error converting value to JTable [closed]

-1

I'm having an error passing value formatted to JTable , if anyone can help, thank you very much.

This error only happens when I format the number, I would like to know how I can correct it.

Code:

    private void calcularButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               
    // TODO add your handling code here:
    // valor < 3.000 = 0.12
    // valor >= 3.000 && valor <= 5.000  = 0.18
    // valor >= 9.000 && valor <= 30.000  = 0.22
    // valor >= 40.000 && valor <= 90.000  = 0.24
    // valor >= 100.000 && valor <= 150.000  = 0.27
    // valor >= 160.000 && valor <= 210.000  = 0.31
    // valor >= 300.000 && valor <= 500.000  = 0.32
    // valor >= 1.000.000 && valor <= 2.000.000  = 0.34

    String posicao = posicaoTextField.getText();
    String nome = nomeTextField.getText();
    float valor = Float.valueOf(valorTextField.getText());
    float valorVenda = 0;

    DefaultTableModel dtm = (DefaultTableModel) valoresTableView.getModel();

    NumberFormat nf = NumberFormat.getCurrencyInstance();
    DecimalFormat df = new DecimalFormat("R$ ###,###.00");  



    if (valor < 3000) {
        valorVenda = (float) (valor * 0.095);
        float valorTotalVenda = valor + valorVenda;
        System.out.println(nf.format(valorTotalVenda));
    }

    if (valor >= 3000 && valor <= 8000 ) {
        valorVenda = (float) (valor * 0.137);
        float valorTotalVenda = valor + valorVenda;
        System.out.println(nf.format(valorTotalVenda));
    }

    if (valor >= 9000 && valor <= 39999) {
        valorVenda = (float) (valor * 0.163);
        float valorTotalVenda = valor + valorVenda;
        System.out.println(nf.format(valorTotalVenda));
    }

    if (valor >= 40000 && valor <= 99999) {
        valorVenda = (float) (valor * 0.185);
        float valorTotalVenda = valor + valorVenda;
        System.out.println(nf.format(valorTotalVenda));
    }

    if (valor >= 100000 && valor <= 159999) {
        valorVenda = (float) (valor * 0.191);
        float valorTotalVenda = valor + valorVenda;
        System.out.println(nf.format(valorTotalVenda));
    }

    if (valor >= 160000 && valor <= 219999) {
        valorVenda = (float) (valor * 0.217);
        float valorTotalVenda = valor + valorVenda;
        System.out.println(nf.format(valorTotalVenda));
    }

    if (valor >= 300000 && valor <= 999999) {
        valorVenda = (float) (valor * 0.223);
        float valorTotalVenda = valor + valorVenda;
        float valorGanho = valorTotalVenda - valor;
        dtm.addRow(new Object[]{nome,
                                posicao,
                                df.format(valor).toString(),
                                df.format(valorTotalVenda),
                                df.format(valorGanho)});
    }

    if (valor >= 1000000 && valor <= 9999999) {
        valorVenda = (float) (valor * 0.239);
        float valorTotalVenda = valor + valorVenda;
        float valorGanho = valorTotalVenda - valor;
        dtm.addRow(new Object[]{nome,
                                posicao,
                                df.format(valor).toString(),
                                df.format(valorTotalVenda),
                                df.format(valorGanho)});
    }



}

And the error that appears is this:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Cannot format given Object as a Number
at java.text.DecimalFormat.format(DecimalFormat.java:507)
at java.text.Format.format(Format.java:157)
at javax.swing.JTable$DoubleRenderer.setValue(JTable.java:5356)
at javax.swing.table.DefaultTableCellRenderer.getTableCellRendererComponent(DefaultTableCellRenderer.java:257)
at javax.swing.JTable.prepareRenderer(JTable.java:5723)
at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:2114)
at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:2016)
at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1812)
at javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
at javax.swing.JComponent.paintComponent(JComponent.java:780)
at javax.swing.JComponent.paint(JComponent.java:1056)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5210)
at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:290)
at javax.swing.RepaintManager.paint(RepaintManager.java:1272)
at javax.swing.JComponent._paintImmediately(JComponent.java:5158)
at javax.swing.JComponent.paintImmediately(JComponent.java:4969)
at javax.swing.RepaintManager$4.run(RepaintManager.java:831)
at javax.swing.RepaintManager$4.run(RepaintManager.java:814)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:814)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:789)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:738)
at javax.swing.RepaintManager.access$1200(RepaintManager.java:64)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1732)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
    
asked by anonymous 23.08.2017 / 20:19

1 answer

1

Let's first simplify your code:

private void calcularButtonActionPerformed(java.awt.event.ActionEvent evt) {
    String posicao = posicaoTextField.getText();
    String nome = nomeTextField.getText();

    DefaultTableModel dtm = (DefaultTableModel) valoresTableView.getModel();

    NumberFormat nf = NumberFormat.getCurrencyInstance();
    DecimalFormat df = new DecimalFormat("R$ ###,###.00");  

    float valor = Float.parseFloat(valorTextField.getText());

    float fator = valor <    3000 ? 0.095f
                : valor <    9000 ? 0.137f
                : valor <   40000 ? 0.163f
                : valor <  100000 ? 0.185f
                : valor <  160000 ? 0.191f
                : valor <  300000 ? 0.217f
                : valor < 1000000 ? 0.223f
                : 0.239f;

    float valorVenda = valor * fator;
    float valorTotalVenda = valor + valorVenda;

    System.out.println(nf.format(valorTotalVenda));
    dtm.addRow(new Object[] {
            nome,
            posicao,
            df.format(valor).toString(),
            df.format(valorTotalVenda).toString(),
            df.format(valorVenda).toString()
    });
}

All this soup of if s can be reduced to that ternary chained operator. Note that by this approach, gaps that were in your original code, such as the 8500 and the 250,000, disappear. The resulting code is much smaller, simpler, and less repetitive. Note the suffix f in non-integer numbers.

I've also put the code that adds a line in dtm out of it all, so it always runs.

I also deleted the variable valorGanho which would always produce the same result as valorVenda . To check, see this:

    float valorTotalVenda = valor + valorVenda;
    float valorGanho = valorTotalVenda - valor;

So, by substituting valorTotalVenda in the second equation, we have valorGanho = valor + valorVenda - valor , and therefore valorGanho = valorVenda .

    
23.08.2017 / 20:45