My Jcombobox appears to be working but when i make any selection within it, the program does not appear to register the change

StackOverflow https://stackoverflow.com/questions/22542660

  •  18-06-2023
  •  | 
  •  

Domanda

Im working on a tax program for a CS class. I'm using neatbeans and programming in java. Im new to programming. My code appears to be working, however when I make a selection other than "single' from my JComboBox, the math doesn't change the way it should. It continues to use the math from the 'single' selection. Im wondering how to fix this problem. Also, there is a random 2000 dollars that seem to float in the math, so something is wrong with my calculation, but for the life of me, I can't figure out what. When the user inputs a lower 'taxable income' the math was always 2000 dollars above what it should be, so I subtracted 2000 to make it work. However, when the user inputs a large amount of 'taxable income' the math is always spot on, making my subtraction of 2000 dollars wrong. If anyone could take a glance at my code and help me out I would appreciate it. PS, only 'single' and 'married filing jointly' are filled in because I was trying to debug the program.

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class IncomeTax2 extends JFrame implements ItemListener
{

    private JTextField jtfIncome;
    private JTextField jtfTax;
    private JButton jbCalculate;
    private JComboBox jcb;
    private JLabel info;
    private String[] fsChoice =
    {
        "", "Single", "Married filing jointly", "Married filing separately",
        "Head of Household"
    };
    int income;
    double tax;
    private String filingStatus;

    public static void main(String[] args)
    {
        JFrame frame = new IncomeTax2();
        frame.pack();
        frame.setTitle("Income Tax Calculator");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public IncomeTax2()
    {
        JPanel p = new JPanel();
        p.setLayout(new GridLayout(3, 2, 5, 5));
        p.setBorder(new EmptyBorder(5, 5, 5, 5));
        p.add(jtfIncome = new JTextField(8));
        p.add(jcb = new JComboBox(fsChoice));
        p.add(info = new JLabel("Enter taxable income "
                + "than pick filing status"));
        p.add(jbCalculate = new JButton("Calculate"));
        p.add(jtfTax = new JTextField(16));
        jtfTax.setEditable(false);
        setLayout(new BorderLayout());
        jtfTax.setHorizontalAlignment(JTextField.RIGHT);
        add(p, BorderLayout.CENTER);

        jtfIncome.addKeyListener(new KeyAdapter()
        {
            public void keyTyped(KeyEvent e)
            {
                char c = e.getKeyChar();
                if (!(Character.isDigit(c) || (c == KeyEvent.VK_BACK_SPACE)
                        || c == KeyEvent.VK_DELETE || c == KeyEvent.VK_ENTER))
                {
                    Toolkit.getDefaultToolkit().beep();
                    System.out.println("User must enter a digit.");
                    e.consume();
                }
            }
        });

        jcb.addItemListener(this);

        jbCalculate.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                calculate();
            }
        });
    }

    public void itemStateChanged(ItemEvent e)
    {
        income = Integer.parseInt(jtfIncome.getText());

        if (e.getStateChange() == ItemEvent.SELECTED)
        {
            filingStatus = (String) jcb.getSelectedItem();
            System.out.println(filingStatus);
            switch (filingStatus)
            {
                case "Single":
                    if (income <= 8350)
                    {
                        tax = income * 0.10;
                    }
                    if (income >= 8351 && income <= 33950)
                    {
                        tax = income * 0.15;
                    }
                    if (income >= 33951 && income <= 82250)
                    {
                        tax = income * 0.25;
                    }
                    if (income >= 82251 && income <= 171550)
                    {
                        tax = income * 0.28;
                    }
                    if (income >= 171551 && income <= 372950)
                    {
                        tax = income * 0.33;
                    } else
                    {
                        tax = income * 0.35;
                    }
                    break;

                case "Married filing jointly":
                    if (income <= 16700)
                    {
                        tax = income * 0.10;
                    }
                    if (income >= 16701 && income <= 67900)
                    {
                        tax = income * 0.15;
                    }
                    if (income >= 67901 && income <= 137050)
                    {
                        tax = income * 0.25;
                    }
                    if (income >= 137051 && income <= 208850)
                    {
                        tax = income * 0.28;
                    }
                    if (income >= 208851 && income <= 372950)
                    {
                        tax = income * 0.33;
                    } else
                    {
                        tax = income * 0.35;
                    }
                    break;
            }
        }
    }

    public void calculate()
    {
        jtfTax.setText((int) (tax - 2000) + "");
    }

}
È stato utile?

Soluzione

I get the error where for example, entering 4000 the tax is 1400, which is 35% of 4000 so it looks like a problem with the if. You can fix this in two ways, the first changing the if

if (income <= 8350){
    tax = income * 0.10;
    break;
}

in every one (don't) and the other option is just putting an

else if (income > 372950)

instead of just an else at the end. This fixes the weird $2000 problem you were having.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top