I'm not sure what the issue is. The top of the console says after the user inputs annual income. There are no errors shown in the console itself.
The program is to find income tax based on annual income and marital status.
This is my code:
import java.util.Scanner;
public class taxpayers {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
String maritalStatus;
System.out.println("What is your marital status? Type 'M' for married and 'S' for single.");
maritalStatus = sc.next(); //reads next input as marital status
System.out.println("What is your annual income?");
double income;
income = sc.nextDouble();
double marriedTax; //declare these doubles before beginning the if/then statements
double singleTax;
if (maritalStatus.equals("M")) {
just small syntax error
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
String maritalStatus;
System.out.println("What is your marital status? Type 'M' for married and 'S' for single.");
maritalStatus = sc.next(); //reads next input as marital status
System.out.println("What is your annual income?");
double income;
income = sc.nextDouble();
double marriedTax = 0; //declare these doubles before beginning the if/then statements
double singleTax;
if (maritalStatus.equals("M")) {
if (income > 0 && income <= 39000) {
marriedTax = income * .15;
}
else if (income > 39000 && income <= 94250) {
marriedTax = (income - 39000) * .28 + 5850;
}
else if (income > 94250 && income <= 143600) {
marriedTax = (income - 94250) * .31 + 21320;
}
else if (income > 143600 && income <= 256500) {
marriedTax = (income - 143600) * .36 + 36618.5;
}
else if (income > 256500) {
marriedTax = (income - 256500) * 39.6 + 77262.5;
}
System.out.printf("Your income taxes are $%.2f.", marriedTax );
};
if (maritalStatus.equals("S")) {
if (income > 0 && income <= 23350) {
singleTax = income * .15;
}
else if (income > 23350 && income <= 56550) {
singleTax = (income - 23350) * .28 + 3502.5;
}
else if (income > 56550 && income <= 117950) {
singleTax = (income - 56550) * .31 + 12798.5;
}
else if (income > 117950 && income <= 256500) {
singleTax = (income - 117950) * .36 + 31832.5;
}
else if (income > 256500); {
singleTax = (income - 256500) * 39.6 + 81710.5;
}
System.out.printf("Your income taxes are $%.2f.", singleTax);
};
sc.close();
}
Your last else if conditions are incorrect
else if (income > 256500); <-- this end the condition
else if (income > 256500); <-- this end the condition
Remove semicolns at the end and use {
try like this
public class taxpayers {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String maritalStatus;
System.out.println("What is your marital status? Type 'M' for married and 'S' for single.");
maritalStatus = sc.next(); //reads next input as marital status
System.out.println("What is your annual income?");
double income;
income = sc.nextDouble();
double marriedTax; //declare these doubles before beginning the if/then statements
double singleTax;
if (maritalStatus.equals("M")) {
if (income > 0 && income <= 39000) {
marriedTax = income * .15;
} else if (income > 39000 && income <= 94250) {
marriedTax = (income - 39000) * .28 + 5850;
} else if (income > 94250 && income <= 143600) {
marriedTax = (income - 94250) * .31 + 21320;
} else if (income > 143600 && income <= 256500) {
marriedTax = (income - 143600) * .36 + 36618.5;
} else if (income > 256500){
marriedTax = (income - 256500) * 39.6 + 77262.5;
}
System.out.printf("Your income taxes are $%.2f.", marriedTax);
};
if (maritalStatus.equals("S")) {
if (income > 0 && income <= 23350) {
singleTax = income * .15;
} else if (income > 23350 && income <= 56550) {
singleTax = (income - 23350) * .28 + 3502.5;
} else if (income > 56550 && income <= 117950) {
singleTax = (income - 56550) * .31 + 12798.5;
} else if (income > 117950 && income <= 256500) {
singleTax = (income - 117950) * .36 + 31832.5;
} else if (income > 256500){
singleTax = (income - 256500) * 39.6 + 81710.5;
}
System.out.printf("Your income taxes are $%.2f.", singleTax);
}
sc.close();
}
}
Related
I am building a program that checks your salary and returns the amount of tax that you will pay for that financial year.
When the user inserts a character instead of an int, the code below prints the error message twice. How do I change that so it only prints the message once.
Any help would be appreciated. I know the error is occurring in the if statement but I do not know how to change it to detect that the input from user is a char or not.
/* this code works bar one thing with
1: When a Char is entered, it prints an error message twice - doesn't do this when a negative number is entered.
*/
import java.util.Scanner;
public class MyProgram {
public static void main(String[] args) {
// call scanner for user input
Scanner in = new Scanner(System.in);
// double for salary
int salary = 0;
double incomeTax = 0;
double weeklyIncome;
do {
System.out.println("Please enter your salary?");
try {
salary = in.nextInt();
// test if user enters something other than an integer
} catch (java.util.InputMismatchException e) {
System.out.println("Invalid input, only enter a number");
salary = Integer.MIN_VALUE; in.next(); // consume the non-int so we don't get caught in an endless loop\
}
if (salary <= 0) {
System.out.println("Invalid input, please enter a number above 0");
}
}
while (salary <= 0); // loop as long as the salary is less than zero
if (salary <= 18000 & salary > 0) {
incomeTax = 0;
System.out.println("your salary is $" + salary + ", you will pay no income tax this financial year.");
} else if (salary >= 18201 & salary <= 45000) {
incomeTax = (0.19 * (salary - 18200));
weeklyIncome = (salary/52);
System.out.println("your weekly income is $" + weeklyIncome);
System.out.println("you will pay $" + incomeTax + " in tax this financial year.");
} else if (salary >= 45001 & salary <= 120000) {
incomeTax = 5062 + (0.325 * (salary - 45000));
weeklyIncome = (salary/52);
System.out.println("your weekly income is $" + weeklyIncome);
System.out.println("you will pay $" + incomeTax + " in tax this financial year.");
} else if (salary >= 120001 & salary <= 180000) {
incomeTax = 29467 + (0.37 * (salary - 120000));
weeklyIncome = (salary/52);
System.out.println("your weekly income is $" + weeklyIncome);
System.out.println("you will pay $" + incomeTax + " in tax this financial year.");
} else if (salary >= 180001) {
incomeTax = 51667 + (0.45 * (salary - 180000));
weeklyIncome = (salary/52);
System.out.println("your weekly income is $" + weeklyIncome);
System.out.println("you will pay $" + incomeTax + " in tax this financial year.");
}
in .close();
}
}
I have a java program that calculates users' income taxes, with some help the project now runs correctly, but I am supposed to add an option for the user to process additional customers once they have finished with a customer by entering 'y' into a prompt. I need the prompt to say: "Process another customer? (y/n)?" Since the user could also hit "n" this prompt will also have to have a command for both 'y' and 'n' how and where do I do this and where do I need to put the prompt in my code? here is my 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 tax = 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") && 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);
}
}
Make use of a do...while loop.
The while and do-while Statements | Java Documentation
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"));
}
}
You can do this in the end of your program, after you display the tax of the first user you can ask if there is a new user or no, if yes you will repeat the same steps as for the first user and if no your program will finish, to do that you must first add a boolean variable (or integer as you want), to know if there is a new user or no:
boolean newCustomer = false;
then your entire code will be inside a do while loop as following:
do{
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") && 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.println("Your tax is: " + tax);
System.out.print("Please type 'y' for a new customer, or 'n' to finish the program : ");
String customer = in.next();
if(customer.equals("y"))
newCustomer = true;
else
newCustomer = false;
}
while(newCustomer);
I'm trying to calculate a Tax and NI calculator. I managed to write the code for it but can't seem to get the GUI working. i have tried to implement many different examples but they seem to give out errors. I just want to keep it simple and i even tried message dialog and a normal GUI screen with a OK button
Code for Calculator: (User enters their income and it calculates the tax etc.)
package netincome;
import java.io.*;
public class NetIncome {
int pan;
String name;
double taxableincome;
double tax;
double taxpermonth;
double annualNIpayments;
double NIpermonth;
double netmonthlyincome;
void input() throws IOException {
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(in);
System.out.println("Enter taxable income:");
taxableincome = Double.parseDouble(br.readLine());
}
void computeData() {
if (taxableincome <= 11000) {
tax = 0;
taxpermonth = tax/12;
annualNIpayments = 0 * 0.00;
NIpermonth = annualNIpayments/12;
netmonthlyincome = ((taxableincome - tax - annualNIpayments)/12);
} else if (taxableincome > 11001 && taxableincome <= 43000) {
tax = (taxableincome * 0.20);
taxpermonth = tax / 12;
annualNIpayments = taxableincome * 0.12;
NIpermonth = annualNIpayments / 12.0;
netmonthlyincome = ((taxableincome - tax - annualNIpayments)/12);
} else if (taxableincome > 43001 && taxableincome <= 150000) {
tax = (taxableincome * 0.40);
taxpermonth = tax / 12.0;
annualNIpayments = taxableincome * 0.02;
NIpermonth = annualNIpayments / 12.0;
netmonthlyincome = ((taxableincome - tax - annualNIpayments)/12);
} else if (taxableincome > 150001) {
tax = (taxableincome * 0.45);
taxpermonth = tax / 12.0;
annualNIpayments = taxableincome * 0.02;
NIpermonth = annualNIpayments / 12.0;
netmonthlyincome = ((taxableincome - tax - annualNIpayments)/12);
}
}
void displayData() {
System.out.println("Taxable Income =" + taxableincome);
System.out.println("Annual Tax Paid =" + tax);
System.out.println("Monthly Tax Paid ="+ taxpermonth);
System.out.println("Annual NI =" + annualNIpayments);
System.out.println("Monthly NI =" + NIpermonth);
System.out.println("Net Monthly Income =" + netmonthlyincome);
}
public static void main(String args[]) throws IOException {
NetIncome ob = new NetIncome();
ob.input();
ob.computeData();
ob.displayData();
}
}
For example i used this website to get the dialog but can't seem to make the if statement to work on it: Message Dialog Code
The other ones were a GUI screen.
Thanks in advance.
i know my code isn't amazing but i'm only a beginner.
A simple way of doing this is:
Replace
System.out.println("Enter taxable income:");
taxableincome = Double.parseDouble(br.readLine());
with:
String input = JOptionPane.showInputDialog(null, "Enter taxable income:");
taxableincome = Double.parseDouble(input);
And then the output (I'll let you finish the rest off)
Replace:
System.out.println("Taxable Income =" + taxableincome);
with:
JOptionPane.showMessageDialog(null, "Taxable Income = "+ taxableincome);
I keep getting this error and I am not stuck trying to fix it.
package bonuscalc;
import java.text.DecimalFormat;
import java.util.Scanner;
public class BonusCalc {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
DecimalFormat formatter = new DecimalFormat("#0.00");
int Salary;
double NewSal, Comm;
double p1 = 0.1;
double p2 = 0.15;
double p3 = 0.2;
double p4 = 0.3;
System.out.println("Welcome to Bonus Calculator");
do{
System.out.print("Enter your Salary: ");
Salary = input.nextInt();
}While (Salary < 0)
if((Salary > 0) && (Salary <= 8000)){
Comm = (Salary * p1);
NewSal = Salary + Comm;
System.out.print("Your Commition is RM" + formatter.format(Comm));
System.out.println(" and your New Salary is RM" + formatter.format(NewSal));
}
else if((Salary > 8000) && (Salary <= 15000)){
Comm = (Salary * p2);
NewSal = Salary + Comm;
System.out.print("Your Commition is RM" + formatter.format(Comm));
System.out.println(" and your New Salary is RM" + formatter.format(NewSal));
}
else if((Salary > 15000) && (Salary <= 25000)){
Comm = (Salary * p3);
NewSal = Salary + Comm;
System.out.print("Your Commition is RM" + formatter.format(Comm));
System.out.println(" and your New Salary is RM" + formatter.format(NewSal));
}
else if(Salary > 25000){
Comm = (Salary * p4);
NewSal = Salary + Comm;
System.out.print("Your Commition is RM" + formatter.format(Comm));
System.out.println(" and your New Salary is RM" + formatter.format(NewSal));
}
else{
System.out.println("Input invalid. Renter Salary");
}
}
}
You have written While instead of while.
do {
...
} While (Salary < 0);
correct would be:
do {
...
} while (Salary < 0);
Hope this solves your problem.
Your do-while loop has an invalid syntax. Firstly, while is lowercase, thus While is incorrect. Moreover, you are missing a semicolon.
do {
System.out.print("Enter your Salary: ");
Salary = input.nextInt();
} while (Salary < 0);
On a side note, in Java variables usually start with lowercase letters. It is not a strict rule, but a convention that it is prudent to conform to.
This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 8 years ago.
I need my output to float to only 2 decimal places as the output is money. For example if I run:
Enter 1 if you are single. Enter 2 if you are married
1
Enter your taxable income
27060.34
Federal Income Tax:$4060.3435
I need the federal income tax to output $4060.34 instead of $4060.3435
My code:
import static org.junit.Assert.*;
import java.util.Scanner;
import org.junit.Test;
public class IRS {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter 1 if you are single. Enter 2 if you are married");
int martialstatus = scan.nextInt();
if (martialstatus == 1) {
Scanner scan2 = new Scanner(System.in);
System.out.println("Enter your taxable income");
double income = scan2.nextDouble();
if ((income > 0) && (income <= 27050.00)) {
System.out.println("Federal Income Tax:$" + (income * .15));
}
if ((income > 27050.00) && (income <= 65550.00)) {
System.out.println("Federal Income Tax:$" + (4057.50 + (.275 * (income - 27050))));
}
if ((income > 65550.00) && (income <= 136750.00)) {
System.out.println("Federal Income Tax:$" + (14645.00 + (.305 * (income - 65550.00))));
}
if ((income > 136750.00) && (income <= 297350.00)) {
System.out.println("Federal Income Tax:$" + (36361.00 + (.355 * (income - 136750.00))));
}
if (income > 297350.00) {
System.out.println("Federal Income Tax:$" + (93374.00 + (.391 * (income - 297350.00))));
}
} else if (martialstatus == 2) {
Scanner scan3 = new Scanner(System.in);
System.out.println("Enter your taxable income");
double income2 = scan3.nextDouble();
if (income2 <= 45200.00) {
System.out.println("Federal Income Tax:$" + (.15 * income2));
}
if ((income2 > 45200.00) && (income2 <= 109250.00)) {
System.out.println("Federal Income Tax:$" + (6780.00 + (.275 * (income2 - 45200))));
}
if ((income2 > 109250.00) && (income2 <= 166500.00)) {
System.out.println("Federal Income Tax:$" + (24393.75 + (.305 * (income2 - 109250.00))));
}
if ((income2 > 166500.00) && (income2 <= 297350.00)) {
System.out.println("Federal Income Tax:$" + (41855.00 + (.355 * (income2 - 166500.00))));
}
if (income2 > 297350.00) {
System.out.println("Federal Income Tax:$" + (88306.00 + (.391 * (income2 - 297350.00))));
}
} else {
System.out.println("Error. Try again");
}
}
}
If you are comfortable with C style printing (which is very much what I'd recommend here) you can use System.out.printf() which is very similar to C's printf().
System.out.printf ("Federal Income Tax: $%.2f", federalTax);
Due to the inherent uncertainty involved with floating point arithmetic, you should always use BigDecimal for mathematical operations where knowing you have the "right" answer is important (tax program would be a good example where that's important).
This question has a good example, take good note of the first answer:
Using BigDecimal to work with currencies
First of all: don't use float or double to calculate with currency values. Use BigDecimal instead.
Second to your question:
To round a value use the following formula (I use double in my example but don't do this if you do not know what you're doing):
public double round(double value) {
return (((int)(value * 10.d))/10.d);
}
This method rounds to one valid decimal place. You have to change this implementation if you want another number of decimal places.
Use DecimalFormat:
java.text.DecimalFormat formatter = new java.text.DecimalFormat( "$###,###,###,##0.00";-$###,###,###,##0.00" );
formatter.format( 4060.3435 );
System.out.println("Federal Income Tax:$" + (double) Math.round((88306.00 +(.391*(income2 - 297350.00))))) /100.0;
As others have said: Either use BigDecimal or -- the better solution in most cases -- do your calculations in pennies rather than dollars. That gets rid of most of the round-off issues immediately!