0

I'm new to programming and need help pulling data out of an if var. I need to pull data from vars in both the if statements and combine them towards the bottom. This is small section of my program that I'm having issues with. This program is used to calculate after tax income using the data provided by a user.

I'm able to pull this off but only if the var is declared right above the if statement. If I declare the var at the top and try to pull the data towards the bottom it does not work. It states var might not have been initialized.

Not a duplicate question. I have search and can not find any answers that resolve this problem. Sure I can initialize the var but I need it to pull data from the IF statement. That was stated above.

This is where my problem is. combTotal += weekStTotal37k;

package test; 
import java.text.DecimalFormat;
import javax.swing.JOptionPane;

public class Test {

public static void main(String[] args) {
    double combTotal;
    double grossSalIncomeDou = 10000;
    double weekIncomeDou9k;
    double weekIncomeDou80k;
    double weekStTotal37k;

    DecimalFormat df = new DecimalFormat ("#,###.00");

        if (grossSalIncomeDou > 9325 && grossSalIncomeDou <= 37950) {
            weekIncomeDou9k = 9325 * .90;
            weekIncomeDou9k /= 52;

            weekIncomeDou80k = grossSalIncomeDou - 9325;
            weekIncomeDou80k *= .85;
            weekIncomeDou80k /= 52;

            combTotal = weekIncomeDou9k + weekIncomeDou80k;
            JOptionPane.showMessageDialog(null, "Weekly income after taxes " + df.format (combTotal));      

        } 

        //If used to calculate income after state tax.
        if (grossSalIncomeDou <= 37110) {
            weekStTotal37k = grossSalIncomeDou * .9435;
            weekStTotal37k /= 52;

        }
            combTotal += weekStTotal37k;
            JOptionPane.showMessageDialog(null, "Weekly income after state and fed taxes " + df.format (combTotal));
}  
}
QJP
  • 1
  • 1

0 Answers0