Results 1 to 13 of 13
  1. #1
    stealthfilms is offline Member
    Join Date
    Feb 2014
    Posts
    4
    Rep Power
    0

    Default Problem with IF Else nesting.

    I have attached my code. The functionality is currently only computing the Gross Income all other text fields are null. When user chooses the status JRadioButton selection the nested loop inside the Calculate() Method should compute the tax. However. Only the "Single" loop is working. I'm sure it's something stupid that I'm missing.

    Thanks for your help.

    Java Code:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    import javax.swing.border.TitledBorder;
    
    public class Taxes extends JFrame {
    
        // create text fields
        private final JTextField jtfLastName = new JTextField();
        private final JTextField jtfMI = new JTextField();
        private final JTextField jtfFirstName = new JTextField();
        private final JTextField jtfSSN = new JTextField();
        private final JTextField jtfDOB = new JTextField();
        private final JTextField jtfGrossIncome = new JTextField("0");
    
        //Create 4 checkboxes for filing status
        private final JRadioButton jchkSingle = new JRadioButton("Single");
        private final JRadioButton jchkMFJ = new JRadioButton("Married Filing Joint");
        private final JRadioButton jchkMFS = new JRadioButton("Married Filing Separate");
        private final JRadioButton jchkHOH = new JRadioButton("Head of Houshold");
    
        // Create a Compute Taxes Button
        // private JButton jbtComputeTax = new JButton(" ----- Compute Taxes ----- ");
        //Window 1 (w1) holds labels for text fields
        public Taxes() {
    
            // Main information panel fields
            JPanel w1 = new JPanel(new GridLayout(0, 4));
            w1.add(new JLabel("Last Name"));
            w1.add(jtfLastName);
            w1.add(new JLabel("Social Security #"));
            w1.add(jtfSSN);
            w1.add(new JLabel("Middle Initial"));
            w1.add(jtfMI);
            w1.add(new JLabel("Date of Birth"));
            w1.add(jtfDOB);
            w1.add(new JLabel("First Name"));
            w1.add(jtfFirstName);
            w1.add(new JLabel("Gross Income*"));
            w1.add(jtfGrossIncome);
            w1.setForeground(Color.red);
            w1.setBorder(new TitledBorder("1st - Enter your information"));
    
            // create a button group
            ButtonGroup group = new ButtonGroup();
            group.add(jchkSingle);
            group.add(jchkMFJ);
            group.add(jchkMFS);
            group.add(jchkHOH);
    
            // Window w3 to hold the CheckBoxes
            JPanel w3 = new JPanel(new FlowLayout(FlowLayout.TRAILING));
            w3.add(jchkSingle);
            w3.add(jchkMFJ);
            w3.add(jchkMFS);
            w3.add(jchkHOH);
            w3.setBorder(new TitledBorder("2nd - Choose your filing status"));
    
            JPanel w4 = new JPanel(new FlowLayout(FlowLayout.LEFT));
            w4.add(new JLabel("Please complete the following information. * Please choose your filing status after entering Gross Income."));
            w4.setBorder(new TitledBorder("INSTRUCTIONS"));
    
            //Add the windows to the frame
            add(w1, BorderLayout.CENTER);
            add(w3, BorderLayout.EAST);
            add(w4, BorderLayout.NORTH);
    
            // Register Listener
            // ButtonListeners
            jchkSingle.addActionListener(new ButtonListener());
            jchkMFJ.addActionListener(new ButtonListener());
            jchkMFS.addActionListener(new ButtonListener());
            jchkHOH.addActionListener(new ButtonListener());
    
        }
         // Calculate class module - FOR TESTING ALL SECTION HAVE SAME MATH 
        private static class Calculate {
    
            public Calculate(double GrossIncome, String status) {
    
                double income;
                double totalTax;
    
                // Tests the status is being passed for each RadioButton
                System.out.println("Calculate Class variable 'status' =" + status);
    
                if ("Single".equals(status)) {
    
                    // If less than or equal to zero message
                    if (GrossIncome <= 0) {
                        JOptionPane.showMessageDialog(null, "Please enter your Gross Income.");
                    }
                    // 10% income tax bracket      
                    if (GrossIncome <= 8350) {
                        totalTax = GrossIncome * 0.10;
                    } // 15% income tax bracket 
                    else if (GrossIncome <= 33950.99) {
                        totalTax = (GrossIncome - 8350.99) * .15 + 835.0;
                    } // 25% income tax bracket 
                    else if (GrossIncome <= 82250.99) {
                        totalTax = (GrossIncome - 33950.99) * .25 + 4674.85;
                    } // 28% income tax bracket 
                    else if (GrossIncome <= 171550) {
                        totalTax = (GrossIncome - 82250.99) * .28 + 16749.60;
                    } // 33% income tax bracket 
                    else if (GrossIncome <= 372950) {
                        totalTax = (GrossIncome - 171550) * .33 + 41753.32;
                    } // 35% income tax bracket 
                    else {
                        totalTax = (GrossIncome - 372950) * .35 + 108214.99;
    
                    }
    
                    if ("Married Filing Joint".equals(status)) {
    
                        // If less than or equal to zero message
                        if (GrossIncome <= 0) {
                            JOptionPane.showMessageDialog(null, "Please enter your Gross Income.");
                        }
                        // 10% income tax bracket      
                        if (GrossIncome <= 8350) {
                            totalTax = GrossIncome * 0.10;
                        } // 15% income tax bracket 
                        else if (GrossIncome <= 33950.99) {
                            totalTax = (GrossIncome - 8350.99) * .15 + 835.0;
                        } // 25% income tax bracket 
                        else if (GrossIncome <= 82250.99) {
                            totalTax = (GrossIncome - 33950.99) * .25 + 4674.85;
                        } // 28% income tax bracket 
                        else if (GrossIncome <= 171550) {
                            totalTax = (GrossIncome - 82250.99) * .28 + 16749.60;
                        } // 33% income tax bracket 
                        else if (GrossIncome <= 372950) {
                            totalTax = (GrossIncome - 171550) * .33 + 41753.32;
                        } // 35% income tax bracket 
                        else {
                            totalTax = (GrossIncome - 372950) * .35 + 108214.99;
                        }
    
                    }
    
                    if ("Married Filing Separate".equals(status)) {
    
                        // If less than or equal to zero message
                        if (GrossIncome <= 0) {
                            JOptionPane.showMessageDialog(null, "Please enter your Gross Income.");
                        }
                        // 10% income tax bracket      
                        if (GrossIncome <= 8350) {
                            totalTax = GrossIncome * 0.10;
                        } // 15% income tax bracket 
                        else if (GrossIncome <= 33950.99) {
                            totalTax = (GrossIncome - 8350.99) * .15 + 835.0;
                        } // 25% income tax bracket 
                        else if (GrossIncome <= 82250.99) {
                            totalTax = (GrossIncome - 33950.99) * .25 + 4674.85;
                        } // 28% income tax bracket 
                        else if (GrossIncome <= 171550) {
                            totalTax = (GrossIncome - 82250.99) * .28 + 16749.60;
                        } // 33% income tax bracket 
                        else if (GrossIncome <= 372950) {
                            totalTax = (GrossIncome - 171550) * .33 + 41753.32;
                        } // 35% income tax bracket 
                        else {
                            totalTax = (GrossIncome - 372950) * .35 + 108214.99;
                        }
    
                    }
    
                    if ("Head of Houshold".equals(status)) {
    
                        // If less than or equal to zero message
                        if (GrossIncome <= 0) {
                            JOptionPane.showMessageDialog(null, "Please enter your Gross Income.");
                        }
                        if (GrossIncome <= 8350) {
                            totalTax = GrossIncome * 0.10;
                        } // 15% income tax bracket
                        else if (GrossIncome <= 33950.99) {
                            totalTax = (GrossIncome - 8350.99) * .15 + 835.0;
                        } // 25% income tax bracket
                        else if (GrossIncome <= 82250.99) {
                            totalTax = (GrossIncome - 33950.99) * .25 + 4674.85;
                        } // 28% income tax bracket
                        else if (GrossIncome <= 171550) {
                            totalTax = (GrossIncome - 82250.99) * .28 + 16749.60;
                        } // 33% income tax bracket
                        else if (GrossIncome <= 372950) {
                            totalTax = (GrossIncome - 171550) * .33 + 41753.32;
                        } // 35% income tax bracket
                        else {
                            totalTax = (GrossIncome - 372950) * .35 + 108214.99;
                        }
                    } else {
                    }
                    JOptionPane.showMessageDialog(null, "Your total tax is $" + totalTax);
    
                }
    
            }
    
        }
    
        // Calculate
        public class ButtonListener implements ActionListener {
    
            public void actionPerformed(ActionEvent e) {
    
                // Get values
                double GrossIncome = Double.parseDouble(jtfGrossIncome.getText());
    
                // Assigns JRadioButton Status to variable 'status'
                JRadioButton status = (JRadioButton) e.getSource();
    
                // JRadioButton Tester
                System.out.println("is returning :" + status.getText());
    
                // Create Income Object
                Calculate tax = new Calculate(GrossIncome, status.getText());
    
            }
    
        }
    
        // Main Method
        public static void main(String[] args) {
            Taxes frame = new Taxes();
            frame.pack();
            frame.setTitle("2009 Tax Calculator");
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
    
        }
    }
    Last edited by stealthfilms; 02-07-2014 at 10:36 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default Re: Problem with IF Else nesting.

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    stealthfilms is offline Member
    Join Date
    Feb 2014
    Posts
    4
    Rep Power
    0

    Default Help with Nested Loops

    I have an issue that I posted in the Beginners thread without any help. Maybe some real Java programmers can help. Please look at it if you think you know what your doing. Thanks

    here is the link

    Problem with IF Else nesting.

  4. #4
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Help with Nested Loops

    Well, it is probably due to the fact that you have no loops in your program.
    1000011 1100001 1101110 100000 1111001 1101111 1110101 100000 1110010 1100101 1100001 1100100 100000 1000010 1101001 1101110 1100001 1110010 1111001 111111

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default Re: Problem with IF Else nesting.

    the nested loop inside the Calculate() Method
    I can't find either the Calculate() method or any loops in the posted code.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Problem with IF Else nesting.

    Quote Originally Posted by Norm View Post
    I can't find either the Calculate() method or any loops in the posted code.
    I think he was reffering to the Calculate constructor, but yes, there are no loops.
    1000011 1100001 1101110 100000 1111001 1101111 1110101 100000 1110010 1100101 1100001 1100100 100000 1000010 1101001 1101110 1100001 1110010 1111001 111111

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,165
    Rep Power
    6

    Default Re: Problem with IF Else nesting.

    Look at the nesting; I'm pretty sure that

    Java Code:
    if ("Married Filing Joint".equals(status)) {
    is not supposed to be INSIDE of the 'single' if block.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default Re: Problem with IF Else nesting.

    If status is not changed in the first if block, then the following ifs would make more sense to be else ifs


    Java Code:
    if ("Married Filing Joint".equals(status)) {
    That is ugly, error prone coding style. Any misspelling in the String means no match. Better to define final Strings with that value and use them in the code vs having a bunch of independently coded Strings.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Problem with IF Else nesting.

    Quote Originally Posted by Norm View Post
    If status is not changed in the first if block, then the following ifs would make more sense to be else ifs


    Java Code:
    if ("Married Filing Joint".equals(status)) {
    That is ugly, error prone coding style. Any misspelling in the String means no match. Better to define final Strings with that value and use them in the code vs having a bunch of independently coded Strings.
    To add, I also prefer this format:
    Java Code:
    if (status.equals("Married Filing Joint"))
    It makes more sense. Comparing the string status to the string "Married Filing Joint" says to me "is this variable equal to this value", but comparing the string "Married Filing Joint" to the string status says to me "is this value equal to this variable". Even though they both do the same thing, I feel like my preferred format is better practice.
    Last edited by zFollette; 02-08-2014 at 06:31 PM.
    1000011 1100001 1101110 100000 1111001 1101111 1110101 100000 1110010 1100101 1100001 1100100 100000 1000010 1101001 1101110 1100001 1110010 1111001 111111

  10. #10
    stealthfilms is offline Member
    Join Date
    Feb 2014
    Posts
    4
    Rep Power
    0

    Default Re: Problem with IF Else nesting.

    Quote Originally Posted by gimbal2 View Post
    Look at the nesting; I'm pretty sure that

    Java Code:
    if ("Married Filing Joint".equals(status)) {
    is not supposed to be INSIDE of the 'single' if block.
    Thank you very much. . . That was the issue and I just couldn't see it as obvious as it was. . . .

  11. #11
    stealthfilms is offline Member
    Join Date
    Feb 2014
    Posts
    4
    Rep Power
    0

    Default Re: Problem with IF Else nesting.

    Thanks zFollette, I like the change in the code. . .
    Last edited by stealthfilms; 02-08-2014 at 07:45 PM.

  12. #12
    notivago is offline Heavy Coffe Drinker
    Join Date
    Feb 2014
    Location
    São Paulo, Brazil
    Posts
    29
    Rep Power
    0

    Default Re: Problem with IF Else nesting.

    Quote Originally Posted by zFollette View Post
    To add, I also prefer this format:
    Java Code:
    if (status.equals("Married Filing Joint"))
    It makes more sense. Comparing the string status to the string "Married Filing Joint" says to me "is this variable equal to this value", but comparing the string "Married Filing Joint" to the string status says to me "is this value equal to this variable". Even though they both do the same thing, I feel like my preferred format is better practice.
    This is more prone to NPE, though and if null is assumed to be a valid value, then you should go for:
    Java Code:
    if (status != null && status.equals("Married Filing Joint"))
    Which is Uglier(TM).

  13. #13
    notivago is offline Heavy Coffe Drinker
    Join Date
    Feb 2014
    Location
    São Paulo, Brazil
    Posts
    29
    Rep Power
    0

Similar Threads

  1. Problem with a Simple Histogram Problem
    By kathmandu in forum New To Java
    Replies: 12
    Last Post: 06-25-2013, 02:19 AM
  2. Replies: 0
    Last Post: 11-07-2012, 01:44 PM
  3. Small problem with problem with Java, C++ parse program.
    By dragstang86 in forum New To Java
    Replies: 4
    Last Post: 10-30-2011, 04:43 AM
  4. Replies: 9
    Last Post: 09-21-2010, 05:15 PM
  5. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 07:12 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •