Domanda

I'm creating a tax project for a CS class. Im working with netbeans. My project is working entirely except for the fact that the return value I always get for the tax is 0.0 . I am new to Java. I think that the problem lies somewhere with my "income" variable. The user inputs their taxable income, I take that string the user entered and parse it into an integer and call it "income". The program then checks the "income" to see if it is greater or less than certain values to see what tax bracket the user is in. The tax bracket is affected by the user's combobox choice for "filling status". So far my program only has the smallest tax bracket and the filling status of single for debugging purposes... once i get this code working I should be able to fill in the rest fairly easily. I'm wondering if anyone can look over the code and see what it is I'm missing. Im having a hard time connecting the combobox with the user jtextfield entry to make a calculation. Im not sure if my problem lies in my array, in my "income" variable, or somewhere else. I've spent a ton of time looking through books and java tutorials. If you can take a glance and help, that would be appreciated :)

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 fillingStatus;

    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(jcb = new JComboBox(fsChoice));
        p.add(jtfIncome = new JTextField(8));
        p.add(info = new JLabel("Pick filling status and "
                + "enter taxable income"));
        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();
            }
        });

         jtfIncome.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                String inputStr = jtfIncome.getText();
                income = Integer.parseInt(inputStr);
            }
        });
    }

    public void itemStateChanged(ItemEvent e)
    {
        if (e.getStateChange() == ItemEvent.SELECTED)
        {
            fillingStatus = (String) jcb.getSelectedItem();
            switch (fillingStatus)
            {
                case "Single":
                    if (income <= 8350)
                    {
                        tax = income * 0.10;
                        System.out.println("case[1]");
                        System.out.println("Tax = " + tax);
                    }
                    ;
                    break;
            }
        }
    }

    public void calculate()
    {
        jtfTax.setText(((int)(tax * 100) / 100.0 + ""));
    }

}
È stato utile?

Soluzione

The ActionListener for the income JTextField relys on the user pressing ENTER to set the income. If this doesnt happen then assignment

tax = income * 0.10;

will produce a value tax equal to 0.

Ensure that the assignment for income occurs at the start of the ItemListener

income = Integer.parseInt(jtfIncome.getText());

before the tax calculation is made.

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