0

just need help with formatting a string to look like this:

Here is your tax breakdown:
Income            $25,000.00
Dependants                 1
----------------------------
Federal Tax        $4,250.00
Provincial Tax     $1,317.75
============================
Total Tax          $5,567.75"

heres my code, please help me format as above with spaces and decimals

import java.util.Scanner;

public class HelloWorld {
    public static void main(String args[]) {
        double income, fedTax, provTax;
        int dependents;

        Scanner input = new Scanner(System.in);
        System.out.print("Please enter your taxable income: ");
        income = input.nextInt();

        System.out.println();
        System.out.print("Please enter your number of dependents: ");

        dependents = input.nextInt();
        System.out.println();

        if (income <= 29590) {
            fedTax = 0.17 * income;
        }

        else if (income <= 59179.99) {
            fedTax = 0.17 * 29590 + 0.26 * (income - 29590);
        }

        else {
            fedTax = 0.17 * 29590 + 0.26 * 29590 + (0.29 * (income - 59180));
        }

        double base = 42.5 * fedTax;
        double deductions = 160.50 + 328 * dependents;
        provTax = base - deductions;
        if (base < deductions) {
            provTax = 0;
        }
        else {
            provTax = base - deductions;
        }

        double totalTax = fedTax + provTax;

        System.out.println("Here is your tax breakdown: ");
        System.out.println(String.format(" Income%,10.d", income));
        System.out.println(String.format(" Dependants%10.d", dependents));
        System.out.println("-------------------------");
        System.out.println(String.format(" Federal Tax%,10.2f", fedTax));
        System.out.println(String.format("Provincial tax%,10.2f", provTax));
        System.out.println("=========================");
        System.out.println(String.format(" Total Tax%,10.2f", totalTax));




    }
}

please ignore down here this is just to let me post because there isnt enough words, even though my question does not to be detailed :(

1 Answers1

1

If the income variable is declared a double data type then you can't expect to fill that variable using the Scanner#nextInt() method which is expecting the User to enter an Integer value whereas you want to enter a double (floating point) value. At the very least you want to use the Scanner#nextDouble() method.

I don't think your calculations for Provincial Tax is correct. If it is then I sure wouldn't want to deal with that bill. I'm not going to try and figure that one out but this is rather suspect to start with:

double base = 42.5 * fedTax;
double deductions = 160.50 + 328 * dependents;
provTax = base - deductions;       // deductions removed from base here...
if (base < deductions) {
    provTax = 0;
}
else {
    provTax = base - deductions;  // and yet again here....hmmmm
}

To get the formatting you want you would want to possibly utilize both the String#format() method along with the DecimalFormat#format() method and possibly a couple of its' other methods (read end note), for example:

// Set the numerical format you want.
java.text.DecimalFormat decimalFormat = new  java.text.DecimalFormat("0.00");
decimalFormat.setGroupingUsed(true); // You want comma grouping
decimalFormat.setGroupingSize(3);    // Comma every 3 digits
    
System.out.println("Here is your tax breakdown: ");
System.out.println(String.format("Income %20s", "$" + decimalFormat.format(income)));
System.out.println(String.format("Dependants %16d", dependents));
System.out.println("---------------------------");
System.out.println(String.format("Federal Tax %15s", "$" + decimalFormat.format(fedTax)));
System.out.println(String.format("Provincial Tax %12s", "$" + decimalFormat.format(provTax)));
System.out.println("===========================");
System.out.println(String.format("Total Tax %17s", "$" + decimalFormat.format(totalTax)));

Your entire code with the above modification and the fix for the income prompt (using the Scanner#nextDouble() method) would look something like this:

double income, fedTax, provTax;
int dependents;

Scanner input = new Scanner(System.in);
System.out.print("Please enter your taxable income: ");
income = input.nextDouble();

System.out.println();
System.out.print("Please enter your number of dependents: ");

dependents = input.nextInt();
System.out.println();

if (income <= 29590) {
    fedTax = 0.17 * income;
}
else if (income <= 59179.99) {
    fedTax = 0.17 * 29590 + 0.26 * (income - 29590);
}
else {
    fedTax = 0.17 * 29590 + 0.26 * 29590 + (0.29 * (income - 59180));
}

double base = 42.5 * fedTax;
double deductions = 160.50 + 328 * dependents;
provTax = base - deductions;
if (base < deductions) {
    provTax = 0;
}
else {
    provTax = base - deductions;
}

double totalTax = fedTax + provTax;

// Set the numerical format you want.
java.text.DecimalFormat decimalFormat = new java.text.DecimalFormat("0.00");
decimalFormat.setGroupingUsed(true); // You want comma grouping
decimalFormat.setGroupingSize(3);    // Comma every 3 digits
  
System.out.println("Here is your tax breakdown: ");
System.out.println(String.format("Income %20s", "$" + decimalFormat.format(income)));
    System.out.println(String.format("Dependents %16d", dependents));
    System.out.println("---------------------------");
    System.out.println(String.format("Federal Tax %15s", "$" + decimalFormat.format(fedTax)));
    System.out.println(String.format("Provincial Tax %12s", "$" + decimalFormat.format(provTax)));
    System.out.println("===========================");
    System.out.println(String.format("Total Tax %17s", "$" + decimalFormat.format(totalTax)));

If you were to run this code now, you should see in the Console Window the following (if you supply the same values):

Please enter your taxable income: 25500.54

Please enter your number of dependents: 1

Here is your tax breakdown: 
Income           $25,500.54
Dependents                1
---------------------------
Federal Tax       $4,335.09
Provincial Tax  $183,752.90
===========================
Total Tax       $188,087.99

What a tax bill for 25.5 grand. :/

Note: You can eliminate the use of the DecimalFormat#setGroupingUsed() and the DecimalFormat#setGroupingSize() methods if you initialize your decimalformat variable with:

DecimalFormat decimalFormat = new DecimalFormat("#,###,##0.00");
DevilsHnd - 退職した
  • 8,739
  • 2
  • 19
  • 22
  • hahaha it is wrong you are right my good sir, I accidentally left a calculation x 45 instead of 45% of it. Thank you for the solution – kowalskianalysis Jul 04 '20 at 06:23