1

I am trying to create a income tax calculator in java, and i got it to run, but it freezes up when i try to put in my marital status. can anybody tell me what I am going wrong? any help would be greatly appreciated. Code:

import java.util.Scanner;

public class TaxCalculator {
    public static void main(String[] args) {
        final double RATE1 = 0.20;
        final double RATE2 = 0.25;
        final double RATE3 = 0.10;
        final double RATE4 = 0.15;
        final double RATE5 = 0.30;
        final double RATE1_SINGLE_LIMIT = 0;
        final double RATE2_MARRIED_LIMIT = 0;
        final double RATE3_COHABITATING_LIMIT = 20000;
        final double RATE4_COHABITATING_LIMIT = 50000;
        double tax1 = 0;
        double tax2 = 0;
        double tax3 = 0;
        double tax4 = 0;
        double tax5 = 0;

        //Enter Income
        Scanner in = new Scanner(System.in);
        System.out.print("Please enter your income: ");
        double income = in.nextDouble();

        System.out.print("Please enter 's' for single, 'm' for married, or 'c' for cohabitating: ");
        String maritalStatus = in.next();

        //Calculate Taxes

        if (maritalStatus.equals("s"));
        //nested if

        if (income > RATE1_SINGLE_LIMIT) {
            tax1 = RATE1 * income;
        }

        if (maritalStatus.equals("m"));
        
        if (income > RATE2_MARRIED_LIMIT) {
            tax2 = RATE2 * income;
        }

        if (maritalStatus.equals("c"));
        
        if (income <= RATE3_COHABITATING_LIMIT) {
            tax3 = RATE3 * income;
        }   
        
        if (maritalStatus.equals("c"));
        
        if (income <= RATE4_COHABITATING_LIMIT) {
            tax4 = RATE4 * income;
        } else {
            tax5= RATE5 * income;
        }
    }
}
homerman
  • 2,899
  • 1
  • 11
  • 27
  • 3
    These comparisons have no effect in the code: `if (maritalStatus.equals("s"));`. – flaxel Feb 10 '21 at 07:54
  • How do I fix that? sorry im new to java – Erik Keener Feb 10 '21 at 08:02
  • @ErikKeener, are you running this app in some online Java compiler? I was not able to reproduce the "freezing" issue – Alex Rudenko Feb 10 '21 at 08:26
  • 1
    You have to step back and carefully research how that scanner works. For starters: when you dont understand what your code is doing, add print statements. like: do a `System.out.println("income is: " + income)` after the input. And then do one for the martialStatus. Your code doesnt "freeze", you simply arent using the scanner as you should. – GhostCat Feb 10 '21 at 08:27

3 Answers3

1

Put in.nextLine(); after double income = in.nextDouble(); and after String maritalStatus = in.next();. This is one of the solutions. The nextLine method will consume the new line (line feed) character, which is created in the input buffer by hitting Enter.

Jiri S.
  • 55
  • 6
0

The nested if statements should look like this (note inverted comparison of String literal to the data input by user):

if ("s".equals(maritalStatus)) {
    if (income > RATE1_SINGLE_LIMIT) {
        tax1 = RATE1 * income;
    }
} else if ("m".equals(maritalStatus)) {
    if (income > RATE2_MARRIED_LIMIT) {
        tax2 = RATE2 * income;
    }
} else if ("c".equals(maritalStatus)) {
    if (income <= RATE3_COHABITATING_LIMIT) {
        tax3 = RATE3 * income;
    }   
    if (income <= RATE4_COHABITATING_LIMIT) {
        tax4 = RATE4 * income;
    } else {
        tax5= RATE5 * income;
    }
}

// add output line
System.out.println("marital status:" +  maritalStatus + "; income=" + income + " -> " + Arrays.asList(tax1, tax2, tax3, tax4, tax5));

Output:

Please enter your income: 3300
Please enter 's' for single, 'm' for married, or 'c' for cohabitating: m
marital status:m; income=3300.0 -> [0.0, 825.0, 0.0, 0.0, 0.0]
Alex Rudenko
  • 15,656
  • 9
  • 13
  • 33
  • Doesnt answer the question. "Freezing" happens because the console is waiting for input This has NOTHING to do with the not-so-great if blocks afterwards. – GhostCat Feb 10 '21 at 08:23
  • 1
    @GhostCat, I could not reproduce "freezing" issue – Alex Rudenko Feb 10 '21 at 08:27
  • Then the OP might have changed the code in the meantime. Anyway, then we are looking at "close for not reproducable" – GhostCat Feb 10 '21 at 08:27
0
  1. fix your if statements and remove redundant taxN variables.
  2. Add in.nextLine() after each Scanner entry.

.

import java.util.Scanner;

public class TaxCalculator {
    static void calculate() {

        final double RATE1 = 0.20;
        final double RATE2 = 0.25;
        final double RATE3 = 0.10;
        final double RATE4 = 0.15;
        final double RATE5 = 0.30;
        final double RATE1_SINGLE_LIMIT = 0;
        final double RATE2_MARRIED_LIMIT = 0;
        final double RATE3_COHABITATING_LIMIT = 20000;
        final double RATE4_COHABITATING_LIMIT = 50000;
        double tax = 0;
        Scanner in = new Scanner(System.in);
        //Enter Income
        System.out.print("Please enter your income: ");
        double income = in.nextDouble();
        in.nextLine();

        System.out.print("Please enter 's' for single, 'm' for married, or 'c' for cohabitating: ");
        String maritalStatus = in.next();
        in.nextLine();

        //Calculate Taxes

        if (maritalStatus.equals("s") && income > RATE1_SINGLE_LIMIT) {
            tax = RATE1 * income;
        } else if (maritalStatus.equals("m") && income > RATE2_MARRIED_LIMIT) {
            tax = RATE2 * income;
        } else if (maritalStatus.equals("c") && income <= RATE3_COHABITATING_LIMIT) {
            tax = RATE3 * income;
        } else if (maritalStatus.equals("c") && income <= RATE4_COHABITATING_LIMIT) {
            tax = RATE4 * income;
        } else {
            tax = RATE5 * income;
        }

        System.out.print("Your tax is: " + tax);

    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String newResponse = "";
        do {
            calculate();
            System.out.println();
            System.out.println("Process another response?. Please enter 'y' for yes, or 'n' for no: ");
            newResponse = in.next();
            in.nextLine();
        } while (newResponse.equals("y"));

    }
}
steven7mwesigwa
  • 1,108
  • 9
  • 18
  • You answer was so helpful, but i just found out I have to add something to it, i am supposed to add an option to this program that " should allow the user to process additional customers as long as he/she enter y in response to a "Process another response" prompt. i get that I will need to put in a print command for the prompt, but how will i get it to allow the user to enter multiple customers? – Erik Keener Feb 10 '21 at 08:45
  • Im just going to post it as a new question so that way if you would like to answer then i could accept you answer again. once again thanks so much for your help – Erik Keener Feb 10 '21 at 08:57
  • @ErikKeener Please do that. I'll be happy to help. – steven7mwesigwa Feb 10 '21 at 08:59
  • @Eric Keener I've modified the code to fit your needs. – steven7mwesigwa Feb 10 '21 at 09:20
  • 1
    oh wow thank you so much! i just posted my question a moment ago ( because I had to wait 90 minutes to post again) but if you put your answer on my new post i would be glad to accept it! thank you for all your help – Erik Keener Feb 10 '21 at 09:23
  • @ErikKeener I've fixed some bug in the code. Please share the question post link. – steven7mwesigwa Feb 10 '21 at 09:33
  • @ErikKeener I've seen it. Posted! – steven7mwesigwa Feb 10 '21 at 09:42