"Variable not initialized"
This is a class that I made to test my if-else statement for calculating withholding tax for gross pay. The error I get is this:
error: variable tax might not have been initialized
System.out.println("Withholding Tax = " + tax);
I initialized the variable tax and covered all of the possible values for gross in the if-else statements. So what am I doing wrong?
Code :
import java.util.Scanner;
class apples
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
double gross = input.nextDouble();
double tax;
if (0 <= gross && gross <= 300.00)
{
tax = 0.10;
}
else if (300.01 <= gross && gross <= 400.00)
{
tax = 0.12;
}
else if (400.01 <= gross && gross <= 500.00)
{
tax = 0.15;
}
else if (gross >= 500.01)
{
tax = 0.20;
}
else
{
System.out.println("ERROR! Gross pay is less than zero");
}
System.out.println("Withholding Tax = " + tax);
}
}
Re: "Variable not initialized"
Hello davis220!
You have not initialized tax, you have declared it You should just give it an initial value (0 is very common for a double).