Issue
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.34 * income;
}
else if (income <= 59179.99) {
fedTax = 0.34 * 29590 + 0.26 * (income - 29590);
}
else {
fedTax = 0.34 * 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));
}
}
one of the main problems that was happening was because I was trying to insert input in my tests using the system.out and printstream to sort of rig the test inputs for this program. Using this it is much easier to allow for multiple inputs in a row when the order of function calls overwrites the buffer for the program.
public class StringInputProvider implements InputProvider {
private StringBuilder inputBuilder = new StringBuilder();
private int currentIndex = 0;
public StringInputProvider(String... inputs) {
for(String input : inputs){
this.inputBuilder.append(input).append(System.lineSeparator());
}
}
public void addInput(String input) {
inputBuilder.append(input).append(System.lineSeparator());
}
public String nextLine() {
int nextLineStart = currentIndex;
while (currentIndex < inputBuilder.length() && inputBuilder.charAt(currentIndex) != '\n') {
currentIndex++;
}
String line = inputBuilder.substring(nextLineStart, currentIndex).trim();
currentIndex++; // Move past the newline character
return line;
}
}
EDIT: Added new values for new version
Solution
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");
Answered By - DevilsHnd - 退した
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.