Reputation: 19
Whenever I try to to an else statement, Eclipse highlights the line and says that the "else" is a syntax error and to delete it. My code looks like this
import java.util.Scanner;
public class P4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
double grossSalary, interestIncome, capitalGains, totalIncome, adjustedIncome, totalTax, stateTax;
int exemptions;
Scanner input = new Scanner(System.in);
System.out.print("Gross Salary: ");
grossSalary = input.nextDouble();
System.out.print("Number of Exemptions: ");
exemptions = input.nextInt();
System.out.print("Interest Income: ");
interestIncome = input.nextDouble();
System.out.print("Capital Gains: ");
capitalGains = input.nextDouble();
totalIncome = grossSalary + interestIncome + capitalGains;
adjustedIncome = totalIncome - (exemptions * 1800.0);
if ((adjustedIncome >= 0.0 && adjustedIncome < 10000.0));
stateTax = adjustedIncome * .06;
totalTax = 0.0 + stateTax;
else if (adjustedIncome >= 10000.0 && adjustedIncome < 25000.0);
stateTax = adjustedIncome * .06;
totalTax = (adjustedIncome * .15) + stateTax;
else if (adjustedIncome >= 25000.0 && adjustedIncome < 36000);
stateTax = adjustedIncome * .06;
totalTax = (adjustedIncome * .25) + stateTax;
else (adjustedIncome >= 36000.0);
stateTax = adjustedIncome * .06;
totalTax = (adjustedIncome * .28) + stateTax;
System.out.printf("Total Income: $ %.2f", totalIncome);
System.out.println();
System.out.printf("Adjusted Income: $ %.2f", adjustedIncome);
System.out.println();
System.out.printf("Total Tax: $ %.2f", totalTax);
System.out.println();
System.out.printf("State Tax: $ %.2f", stateTax);
}
}
The else if statements are coming back as errors as well. I don't understand what's wrong with the statements, because even when I try to put an else directly after the if, it still says that theres a syntax error.
Upvotes: 0
Views: 1775
Reputation: 82297
Following an if statement, there are a few ways to execute the then code.
{}
;
In your code, by using if ((adjustedIncome >= 0.0 && adjustedIncome < 10000.0));
the semicolon acts as the first line of executed code (even though there is nothing in between the )
and ;
) and that is the end of the conditional statement.
Following that, there are two lines of code which execute without problem and then an else
is discovered and it causes the error.
stateTax = adjustedIncome * .06;
totalTax = 0.0 + stateTax;
else if (adjustedIncome >= 10000.0 && adjustedIncome < 25000.0);
In order to use else, the if
statement must have been followed by either {}
or nothing.
if(condition) else {
}
if(condition){
}else{
}
Essentially you were looking for the latter here since you have multiple lines of code you want to execute which requires the use of {}
for your if statement.
if ((adjustedIncome >= 0.0 && adjustedIncome < 10000.0)){
stateTax = adjustedIncome * .06;
totalTax = 0.0 + stateTax;
}else if (adjustedIncome >= 10000.0 && adjustedIncome < 25000.0){
stateTax = adjustedIncome * .06;
totalTax = (adjustedIncome * .15) + stateTax;
}else if (adjustedIncome >= 25000.0 && adjustedIncome < 36000){
stateTax = adjustedIncome * .06;
totalTax = (adjustedIncome * .25) + stateTax;
}else if (adjustedIncome >= 36000.0){
stateTax = adjustedIncome * .06;
totalTax = (adjustedIncome * .28) + stateTax;
}
Note that else
works similar to if()
so when nothing follows else
the first code line is executed which is the if(){}
code block. The else could also have looked like this
}else{
if (adjustedIncome >= 36000.0){
stateTax = adjustedIncome * .06;
totalTax = (adjustedIncome * .28) + stateTax;
}
}
Upvotes: 0
Reputation: 3592
You need to use brackets to delimite the ambit of IF and IF ELSE
import java.util.Scanner;
public class P4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
double grossSalary, interestIncome, capitalGains, totalIncome, adjustedIncome, totalTax, stateTax;
int exemptions;
Scanner input = new Scanner(System.in);
System.out.print("Gross Salary: ");
grossSalary = input.nextDouble();
System.out.print("Number of Exemptions: ");
exemptions = input.nextInt();
System.out.print("Interest Income: ");
interestIncome = input.nextDouble();
System.out.print("Capital Gains: ");
capitalGains = input.nextDouble();
totalIncome = grossSalary + interestIncome + capitalGains;
adjustedIncome = totalIncome - (exemptions * 1800.0);
if ((adjustedIncome >= 0.0 && adjustedIncome < 10000.0)) {
stateTax = adjustedIncome * .06;
totalTax = 0.0 + stateTax;
} else if (adjustedIncome >= 10000.0 && adjustedIncome < 25000.0) {
stateTax = adjustedIncome * .06;
totalTax = (adjustedIncome * .15) + stateTax;
} else if (adjustedIncome >= 25000.0 && adjustedIncome < 36000) {
stateTax = adjustedIncome * .06;
totalTax = (adjustedIncome * .25) + stateTax;
} else if (adjustedIncome >= 36000.0) {
stateTax = adjustedIncome * .06;
totalTax = (adjustedIncome * .28) + stateTax;
}
System.out.printf("Total Income: $ %.2f", totalIncome);
System.out.println();
System.out.printf("Adjusted Income: $ %.2f", adjustedIncome);
System.out.println();
System.out.printf("Total Tax: $ %.2f", totalTax);
System.out.println();
System.out.printf("State Tax: $ %.2f", stateTax);
}
}
Upvotes: 0
Reputation: 178263
Two things:
if
or else if
or else
condition. This semicolon will act as the block of the if
, doing nothing, and the statements following it will always be executed.Place braces around the section of code that is to be applied to the if
, else if
, or else
. Without them, even without the semicolon that was removed in (1) above, only the first statement will be taken as the block applied to the condition.
if ((adjustedIncome >= 0.0 && adjustedIncome < 10000.0)) { stateTax = adjustedIncome * .06; totalTax = 0.0 + stateTax; } else if (adjustedIncome >= 10000.0 && adjustedIncome < 25000.0) { // and so on
Also, there is no condition on the else
block.
}
else
{
Upvotes: 0
Reputation: 1926
That's because you're immediatly closing your if
condition with a ;
, making the else if
condition invalid.
What you want is:
if ((adjustedIncome >= 0.0 && adjustedIncome < 10000.0)){
stateTax = adjustedIncome * .06;
totalTax = 0.0 + stateTax;
}
else if (adjustedIncome >= 10000.0 && adjustedIncome < 25000.0){
stateTax = adjustedIncome * .06;
totalTax = (adjustedIncome * .15) + stateTax;
}
else if (adjustedIncome >= 25000.0 && adjustedIncome < 36000){
stateTax = adjustedIncome * .06;
totalTax = (adjustedIncome * .25) + stateTax;
}
Upvotes: 0
Reputation: 159794
Remove the semi-colons and use braces
if ((adjustedIncome >= 0.0 && adjustedIncome < 10000.0)) {
stateTax = adjustedIncome * .06;
totalTax = 0.0 + stateTax;
} ...
Upvotes: 1
Reputation: 3281
You cannot use condition with else
:
...
else (condition) {
...
}
will give you compiler error. Another problem is that you put semicolons after each if
or else if
while you should be using curly brackets {}
Upvotes: 3
Reputation: 2586
Get rid of the semicolon:
if ((adjustedIncome >= 0.0 && adjustedIncome < 10000.0));
and use curly braces to properly define the scope of your if block.
if { ... } else { ... }
Upvotes: 0