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);
Related
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 have a project in my programming class that has to give a PayRoll scenario which contains three classes including PayRoll, PayRollTester, and EmployeeRecord. My code compiles and prints the EmployeeRecord correctly but does not list the PayRoll correctly. Instead, I receive an error saying Error: Out of Bounds of ArrayList.
Payroll:
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Date;
// ...javadoc...
public class Payroll
{
//ArrayLists that will use methods from EmployeeRecords.
ArrayList<String> employeeNames2;
ArrayList<Double> employeeWages2;
ArrayList<Integer> emID = new ArrayList<Integer>();
ArrayList<Double> hours = new ArrayList<Double>();
ArrayList<Double> totalPay = new ArrayList<Double>();
//Creating the hours and wages variables.
private double hoursWorked = 0.0;
private double hoursWorked2 = 0.0;
private int weeks = 0;
private String employeeID = "%03d";
private int quit = 1000;
private int i = 1;
Scanner input = new Scanner(System.in);
public void setEmployeePayroll()
{
// Constructs a new EmployeeRecord.
EmployeeRecord e = new EmployeeRecord();
e.setEmployeeInfo();
employeeNames2 = e.getEmployeeNamesArrayList();
employeeWages2 = e.getWageArrayList();
// Local variables used in setEmployeePayroll.
double totalPay2 = 0.0;
double totalHours = 0.0;
double overTime = 0.0;
double overTime2 = 0.0;
System.out.println("Please enter ACME employee ID, the week they worked (1 or 2), and the number of hours worked. This information should be entered in the order the names were entered. Enter 0 when you are done inputing information.");
while(quit != 0)
{
quit = input.nextInt();
if(quit == 0)
{
break;
}
weeks = input.nextInt();
if(weeks == 1)
{
hoursWorked = input.nextDouble();
}
else if(weeks == 2)
{
hoursWorked2 = input.nextDouble();
}
/*
* I am checking to see if the employee is going to be paid for overtime and also calculating the pay for both weeks.
* 1) My first if statement indicates whether or not the employee worked over 40 in week one and week two.
* 2) My first else if statement indicates whether the employee works more than 40 hours in week two but not week one.
* 3) My second else if statement indicates whether the employee works more than 40 hours in week one but not in week two.
* 3) My third else if statement finally indicates that the employee worked over 40 hours in both week one and two.
*/
if(hoursWorked > 0 && hoursWorked <= 40 && hoursWorked2 > 0 && hoursWorked2 <= 40)
{
totalHours = hoursWorked + hoursWorked2;
hours.add(totalHours);
totalPay2 = totalHours * (employeeWages2.get(i - 1));
totalPay.add(totalPay2);
hoursWorked = 0.0;
hoursWorked2 = 0.0;
}
else if(hoursWorked2 > 40 && hoursWorked > 0 && hoursWorked <= 40)
{
overTime2 = hoursWorked2 - 40;
totalHours = hoursWorked + hoursWorked2;
hours.add(totalHours);
totalPay2 = totalHours * (employeeWages2.get(i - 1)) + (overTime2 * 1.5);
totalPay.add(totalPay2);
hoursWorked = 0.0;
hoursWorked2 = 0.0;
}
else if(hoursWorked > 40 && hoursWorked2 <= 40 && hoursWorked2 > 0)
{
overTime = hoursWorked - 40;
totalHours = hoursWorked + hoursWorked2;
hours.add(totalHours);
totalPay2 = totalHours * (employeeWages2.get(i - 1)) + (overTime * 1.5);
totalPay.add(totalPay2);
hoursWorked = 0.0;
hoursWorked2 = 0.0;
}
else if(hoursWorked > 40 && hoursWorked2 > 40)
{
overTime = hoursWorked - 40;
overTime2 = hoursWorked2 - 40;
totalHours = hoursWorked + hoursWorked2;
hours.add(totalHours);
totalPay2 = totalHours * (employeeWages2.get(i - 1)) + (1.5 * (overTime + overTime2));
totalPay.add(totalPay2);
hoursWorked = 0.0;
hoursWorked2 = 0.0;
}
i = quit;
}
System.out.println();
System.out.println("Employee Number | Employee Name | Hours Worked | Total Pay");
for(int i = 0; i < e.getEmployeeNamesArrayList().size();)
{
System.out.println(String.format(employeeID, i + 1) + " | " + emID.get(i + 1) + " | " + hours.get(i + 1) + " | " + totalPay.get(i + 1));
}
}
}
EmployeeRecord:
import java.util.Scanner;
import java.util.ArrayList;
// ...javadoc...
public class EmployeeRecord
{
/*
* Creating the array and instance variables for EmployeeRecord consisting
* of TaxID numbers, Employee Names, Wages, Employee ID numbers and hours worked.
*/
ArrayList<String> employeeNames = new ArrayList<String>();
ArrayList<String> taxIDList = new ArrayList<String>();
ArrayList<Double> employeeWages = new ArrayList<Double>();
Scanner input = new Scanner(System.in);
private String employeeID = "%03d";
private String employeeFullName = " ";
private String taxID = " ";
private double wage = 0.0;
//Adding employees.
public void setEmployeeInfo()
{
System.out.println("Please enter the full names of each ACME employee, their employee tax ID, and their employee wage rate. Type 'Q Q' when you are done entering employee information.");
while(employeeFullName != "Q")
{
employeeFullName = input.next() + " " + input.next();
if(employeeFullName.equalsIgnoreCase("Q" + " " + "Q"))
{
break;
}
taxID = input.next();
wage = input.nextDouble();
employeeNames.add(employeeFullName);
taxIDList.add(taxID);
employeeWages.add(wage);
System.out.println("Employee ID | Employee Name | Tax ID | Wage");
for(int i = 1; i <= employeeNames.size(); i++)
{
System.out.printf(String.format(employeeID, i) + " | " + employeeNames.get(i - 1) + " | " + taxIDList.get(i - 1) + " | " + "%1.2f",employeeWages.get(i - 1));
System.out.println();
}
}
}
/**
* Creating a method that returns the employee ArrayList employeeName.
*/
public ArrayList<String> getEmployeeNamesArrayList()
{
return employeeNames;
}
/**
* Creating a method that returns the employee's Tax ID ArrayList taxIDList.
*/
public ArrayList<String> getTaxIdsArrayList()
{
return taxIDList;
}
/**
* Creating a method that returns the wages ArrayList
*/
public ArrayList<Double> getWageArrayList()
{
return employeeWages;
}
}
PayrollTester:
import java.util.Scanner;
// ...javadoc...
public class PayrollTester
{
public static void main(String[] args)
{
Payroll employeeComplete = new Payroll();
employeeComplete.setEmployeePayroll();
}
}
ArrayList index starts from 0. since your for loop start i = 0, you cannot do (i+1) in accessing arrayList elements.
emID.get(i + 1)
and also the size of emID is always 0 because you have just create new Arraylist but not added any objects to that list.
I have to create a program which has to include three or more classes including my Main class which will run the class simultaneously. Right now, I have a class called EmployeeRecord, which will create arrays for the employees information (employee full name, tax ID, employee ID, and their wage), and another class called Payroll, which is the class I am having the most trouble. In my if and else if statements, I am getting an error when trying to compile stating "incompatible types: double cannot be converted to java.util.ArrayList." I cannot quite understand or find a fix to solve the problem. I have posted my two classes for you. Thanks.
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Date;
/**
*
* #Lab Three
* #author Joseph Malachosky
* #version 9/14/2015
*/
public class Payroll
{
//ArrayLists that will use methods from EmployeeRecords.
ArrayList<String> employeeNames2;
ArrayList<Double> employeeWages2;
ArrayList<Integer> emID = new ArrayList<Integer>();
ArrayList<Double> hours = new ArrayList<Double>();
ArrayList<Double> totalPay = new ArrayList<Double>();
//Creating the hours and wages variables.
private double hoursWorked = 0.0;
private double hoursWorked2 = 0.0;
private int weeks = 0;
private String employeeID = "%03d";
private int quit = 1000;
private int i = 0;
Scanner input = new Scanner(System.in);
public void setEmployeePayroll()
{
// Constructs a new EmployeeRecord.
EmployeeRecord e = new EmployeeRecord();
e.setEmployeeInfo();
employeeNames2 = e.getEmployeeNamesArrayList();
employeeWages2 = e.getWageArrayList();
// Local variables used in setEmployeePayroll.
double totalPay2 = 0.0;
double totalHours = 0.0;
double overTime = 0.0;
double overTime2 = 0.0;
System.out.println("Please enter ACME employee ID, the week they worked (1 or 2), and the number of hours worked. This information should be entered in the order the names were entered. Enter 0 when you are done inputing information.");
while(quit != 0)
{
quit = input.nextInt();
if(quit == 0)
{
break;
}
weeks = input.nextInt();
if(weeks == 1)
{
hoursWorked = input.nextDouble();
}
else if(weeks == 2)
{
hoursWorked2 = input.nextDouble();
}
/*
* I am checking to see if the employee is going to be paid for overtime and also calculating the pay for both weeks.
* 1) My first if statement indicates whether or not the employee worked over 40 in week one and week two.
* 2) My first else if statement indicates whether the employee works more than 40 hours in week two but not week one.
* 3) My second else if statement indicates whether the employee works more than 40 hours in week one but not in week two.
* 3) My third else if statement finally indicates that the employee worked over 40 hours in both week one and two.
*/
if(hoursWorked > 0 && hoursWorked <= 40 && hoursWorked2 > 0 && hoursWorked2 <= 40)
{
totalHours = hoursWorked + hoursWorked2;
hours.add(totalHours);
totalPay = totalHours * (employeeWages2.get(i - 1));
totalPay.add(totalPay2);
hoursWorked = 0.0;
hoursWorked2 = 0.0;
}
else if(hoursWorked2 > 40 && hoursWorked > 0 && hoursWorked <= 40)
{
overTime2 = hoursWorked2 - 40;
totalHours = hoursWorked + hoursWorked2;
hours.add(totalHours);
totalPay = totalHours * (employeeWages.get(i - 1)) + (overTime2 * 1.5);
tPay.add(totalPay);
hoursWorked = 0.0;
hoursWorked2 = 0.0;
}
else if(hoursWorked > 40 && hoursWorked2 <= 40 && hoursWorked2 > 0)
{
overTime = hoursWorked - 40;
totalHours = hoursWorked + hoursWorked2;
hours.add(totalHours);
totalPay = totalHours * (employeeWages.get(i - 1)) + (overTime * 1.5);
tPay.add(totalPay);
hoursWorked = 0.0;
hoursWorked2 = 0.0;
}
else if(hoursWorked > 40 && hoursWorked2 > 40)
{
overTime = hoursWorked - 40;
overTime2 = hoursWorked2 - 40;
totalHours = hoursWorked + hoursWorked2;
hours.add(totalHours);
totalPay = totalHours * (employeeWages.get(i - 1)) + (1.5 * (overTime + overTime2));
tPay.add(totalPay);
hoursWorked = 0.0;
hoursWorked2 = 0.0;
}
i = terminate;
}
System.out.println();
System.out.println("Employee Number | Employee Name | Hours Worked | Total Pay");
for(int i = 1; i <= e.getEmployeeNamesArrayList().size(); i++)
{
System.out.println(String.format(employeeID, i) + " | " + emID.get(i - 1) + " | " + hours.get(i - 1) + " | " + totalPay.get(i - 1));
}
}
}
import java.util.Scanner;
import java.util.ArrayList;
/**
* Creating the objects and getMethods for each item that is stored in EmployeeRecord.
* #Lab Three
* #author Joseph Malachosky
* #version 9/14/2015
*/
public class EmployeeRecord
{
/*
* Creating the array and instance variables for EmployeeRecord consisting
* of TaxID numbers, Employee Names, Wages, Employee ID numbers and hours worked.
*/
ArrayList<String> employeeNames = new ArrayList<String>();
ArrayList<String> taxIDList = new ArrayList<String>();
ArrayList<Double> employeeWages = new ArrayList<Double>();
Scanner input = new Scanner(System.in);
private String employeeID = "%03d";
private String employeeFullName = " ";
private String taxID = " ";
private double wage = 0.0;
//Adding employees.
public void setEmployeeInfo()
{
System.out.println("Please enter the full names of each ACME employee, their employee tax ID, and their employee wage rate. Press Q when you are done entering employee information.");
while(employeeFullName != "Q")
{
employeeFullName = input.next();
if(employeeFullName == "Q")
{
break;
}
taxID = input.next();
wage = input.nextDouble();
employeeNames.add(employeeFullName);
taxIDList.add(taxID);
employeeWages.add(wage);
System.out.println("Employee ID | Employee Name | Tax ID | Wage");
for(int i = 1; i <= employeeNames.size(); i++)
{
System.out.printf(String.format(employeeID, i) + " | " + employeeNames.get(i - 1) + " | " + taxIDList.get(i - 1) + " | " + "%1.2f",employeeWages.get(i - 1));
System.out.println();
}
}
}
/**
* Creating a method that returns the employee ArrayList employeeName.
*/
public ArrayList<String> getEmployeeNamesArrayList()
{
return employeeNames;
}
/**
* Creating a method that returns the employee's Tax ID ArrayList taxIDList.
*/
public ArrayList<String> getTaxIdsArrayList()
{
return taxIDList;
}
/**
* Creating a method that returns the wages ArrayList
*/
public ArrayList<Double> getWageArrayList()
{
return employeeWages;
}
}
You have
ArrayList<Double> totalPay = new ArrayList<Double>();
and then:
totalPay = totalHours * (employeeWages2.get(i - 1));
Does your compiler not tell you the line number?
i've found your mistake.
change:
totalPay = totalHours * (employeeWages2.get(i - 1));
to:
totalPay2 = totalHours * (employeeWages2.get(i - 1));
I tried write a table that displays the taxable income for an incremental taxable income from 50,0000 to 60,000 under 4 different filing categories. Employing a method, the return statement in the code only printed out one of the four filing categories columns. Please how can I get the remaining columns printed out?
public class TaxableIncome {
public static void main(String[] args) {
System.out.println("Taxable Income\t\t Single\t\t Married Joint\t\t MarriedSeerate\t\t HeadofaHouse");
System.out.println("-----------------------------------------------------------------------------------------------------");
double Tincome;
int profile=1;
for(Tincome=50000; Tincome<=60000; Tincome+=50) {
System.out.println( Tincome +"\t\t" + computetax(profile, Tincome));
}
}
public static double computetax( int status , double income) {
double tax;
double single=0;
double mjoint=0;
double mseperate=0;
double head=0;
for(status=1;status<=4;status++) {
if(status==1) {
tax = 8350*.10 + (33950-8350)*0.15 + (income- 33950)*0.25;
single= tax;
}
if(status==2) {
tax = 16700*0.10 + (income-16700)*0.15;
mjoint = tax;
}
if(status==3 ) {
tax = 8350*0.10 + (33950-8350)*0.15 + (income-33950)*0.25;
mseperate= tax;
}
if(status ==4){
tax = 11950*0.10 + (45500-11950)*0.15 +(income-45500)* 0.25;
head =tax;
}
}
return (single);
}
}
Or use System.out.print() to output each value as they're calculated within computeTax, then do a System.out.println(); to get a carriage return.
You should roll all of the calculation results into their own object and return one of those.
static class TaxDetails {
double single = 0;
double mjoint = 0;
double mseperate = 0;
double head = 0;
}
public static TaxDetails computetax(double income) {
TaxDetails details = new TaxDetails();
details.single = 8350 * .10 + (33950 - 8350) * 0.15 + (income - 33950) * 0.25;
details.mjoint = 16700 * 0.10 + (income - 16700) * 0.15;
details.mseperate = 8350 * 0.10 + (33950 - 8350) * 0.15 + (income - 33950) * 0.25;
details.head = 11950 * 0.10 + (45500 - 11950) * 0.15 + (income - 45500) * 0.25;
return details;
}
public void test() {
System.out.println("Taxable Income\t\t Single\t\t Married Joint\t\t MarriedSeerate\t\t HeadofaHouse");
System.out.println("-----------------------------------------------------------------------------------------------------");
for (double income = 50000; income <= 60000; income += 50) {
TaxDetails tax = computetax(income);
System.out.println(income + "\t\t" + tax.single + "\t\t" + tax.mjoint + "\t\t" + tax.mseperate + "\t\t" + tax.head);
}
}
You are calculating your value for tax four times, then discarding that and returning just the single value for single.
Move the loop on status out of computetax and into caller.
EDIT: given that you're on a self-study course, here's a version that should work within your constraints.
It still needs a lot of improvement, but will get you further.
public class TaxableIncome {
public static void main(String[] args) {
System.out.println("Taxable Income\t\t Single\t\t Married Joint\t\t MarriedSeerate\t\t HeadofaHouse");
System.out.println("-----------------------------------------------------------------------------------------------------");
double Tincome;
int profile=1;
for(Tincome=50000; Tincome<=60000; Tincome+=50) {
double single=computetax(1, Tincome);
double joint=computetax(2, Tincome);
double seperate=computetax(3, Tincome);
double head=computetax(4, Tincome);
System.out.println( Tincome +"\t\tsingle:\t" + single + "\tjoint:\t" + joint + "\tseparate:\t" + separate + "\thead:\t" + head);
}
}
public static double computetax( int status , double income) {
double tax;
double single=0;
double mjoint=0;
double mseperate=0;
double head=0;
if(status==1) {
tax = 8350*.10 + (33950-8350)*0.15 + (income- 33950)*0.25;
single= tax;
}
if(status==2) {
tax = 16700*0.10 + (income-16700)*0.15;
mjoint = tax;
}
if(status==3 ) {
tax = 8350*0.10 + (33950-8350)*0.15 + (income-33950)*0.25;
mseperate= tax;
}
if(status ==4){
tax = 11950*0.10 + (45500-11950)*0.15 +(income-45500)* 0.25;
head =tax;
}
}
return (single);
}
}
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();
}
}