Home > database >  Tax income program if statement issue
Tax income program if statement issue

Time:09-05

    import java.util.Scanner;
public class CardenasE_IncomeTax
{
public static void main(String[] args)
 {
   String username = "CardenasE";
   String realpass = "CarErick12!";
   boolean isMarried = true;
   boolean isNotMarried = false;
   
   double tax1 = 0;
   double tax2 = 0;
   double tax3 = 0;
   
   double RATE1 = 0.10;
   double RATE2 = 0.12;
   double RATE3 = 0.22;
   double RATE4 = 0.24;
   double RATE5 = 0.32;
   
   double limitSingle1 = 10000;
   double limitSingle2 = 40000;
   double limitSingle3 = 90000;
   double limitSingle4 = 165000;
  
   
   double limitMarried1 = 20000;
   double limitMarried2 = 80000;
   double limitMarried3 = 170000;
 
   Scanner input = new Scanner(System.in);
   
   System.out.println("Username: ");
   String user = input.nextLine();
   
   System.out.println("Password:");
   String pass = input.nextLine();
   
   if (user.equals(username) && pass.equals(realpass))
   {
   System.out.println("Login Successful!");
   }
   else if (!user.equals(username) || !pass.equals(realpass)){
   
   System.out.println("Invalid Credentials!");
    return;}
   
   System.out.printf("%n What is your gross income?: ");
   double grossIncome = input.nextInt();
   
   System.out.println("Are you married?: (Type 's' for single and 'm' for married.) ");
   String maritalStatus = input.next();
   
   if (maritalStatus.equals("s")){
   
     if (grossIncome <=limitSingle1)
     tax1 = grossIncome * RATE1;
     System.out.println("Your income is: "   tax1);
      
      return;}
      else if (grossIncome > limitSingle1 && grossIncome <= limitSingle2){
      
         tax2 = grossIncome * RATE2;
         System.out.println("Your income is: "   tax2);
      }
  
      
   
     }
   
   }
   
  
  
 
  
   
 

When used the bracket for the first limit witch is 10% from 0 to 10,000 it gives the proper solution, but I had another if statement which is greater than 10,000 but less than or equals to 40,000 and its set up as the first if statement but It gives out 0 as the answer, no matter how much numbers I put on.

CodePudding user response:

Try to move the else if block of gross income, to the nested if in statement if (grossIncome <=limitSingle1). Like this:

    if (maritalStatus.equals("s")) {
        if (grossIncome <=limitSingle1) {
            tax1 = grossIncome * RATE1;
            System.out.println("Your income is: "   tax1);
        } else if (grossIncome > limitSingle1 && grossIncome <= limitSingle2) {
            tax2 = grossIncome * RATE2;
            System.out.println("Your income is: "   tax2);
        }  
        return;
    

We can improve your code further by removing tax1, tax2, and tax3 variables and only have tax variable. This is to reduce duplicate print outs.

    if (maritalStatus.equals("s")) {
        if (grossIncome <=limitSingle1) {
            tax = grossIncome * RATE1;
        } else if (grossIncome > limitSingle1 && grossIncome <= limitSingle2) {
            tax = grossIncome * RATE2;
        }  
        System.out.println("Your income is: "   tax);
        return;

As best practice, if you have values that shouldn't change. You should need to add the final keyword to avoid modification.

double final RATE1 = 0.10;

Furthermore, you can improve this by moving your constants in a Constants.java file. You can implement this too as a HashMap to do lookups.

  •  Tags:  
  • java
  • Related