I\'m trying to build a Java tax calculator using GUI. When I type in the first i
ID: 3645116 • Letter: I
Question
I'm trying to build a Java tax calculator using GUI.When I type in the first input, it terminates. What am I doing wrong here?
import java.util.Scanner;
import javax.swing.JOptionPane;
public class TaxGUI {
public static void main(String[] args) {
final double Dependant_Deduction = 2000.00;
final double Senior_Deduction = 0.50;
final double Minor_Deduction = 0.25;
final double Single_Deduction = 500.0;
final double Married_Deduction = 750.0;
Scanner reader = new Scanner(System.in);
//Variables
double grossIncome;//User's input
int numDependant;//User's input
int age; //User's input
int maritalStatus; // User's input
double amountPaid; //User's input
//Request input from the user
//Calculate Tax Rate
String taxrateString = JOptionPane.showInputDialog("Enter your gross income: ");
grossIncome = reader.nextDouble();
double taxrateDouble = Double.parseDouble(taxrateString);
double taxrate = taxrateDouble;
if (grossIncome <= 10000)
{
taxrate = 0.18;
}
else if (grossIncome <= 50000)
{
taxrate = 0.31;
}
else
{
taxrate = 0.40;
}
//Request input from the user
//Calculate Deduction for children
String deduction1String = JOptionPane.showInputDialog("Enter your dependants: ");
numDependant = reader.nextInt();
double deduction1 = Double.parseDouble(deduction1String);
deduction1 = numDependant * Dependant_Deduction;
//Request input from the user
//Calculate Deduction for age
String deduction2String = JOptionPane.showInputDialog("Enter your age: ");
age = reader.nextInt();
double deduction2 = Double.parseDouble(deduction2String);
if(age >= 65)
{
deduction2 = (grossIncome - deduction1) * Senior_Deduction;
}
else if(age <= 17)
{
deduction2 = (grossIncome - deduction1) * Minor_Deduction;
}
else
{
deduction2 = 0;
}
//Request input from the user
//Calculate Deduction for martial status
String deduction3String = JOptionPane.showInputDialog("Enter your marital status(1=single 2=married): ");
maritalStatus = reader.nextInt();
double deduction3 = Double.parseDouble(deduction3String);
if(maritalStatus == 1)
{
deduction3 = Single_Deduction;
}
else if(maritalStatus == 2)
{
deduction3 = Married_Deduction;
}
//Request input from the user for the tax paid
String PaidString = JOptionPane.showInputDialog("Enter the amount you paid in taxes during the year: ");
amountPaid = reader.nextDouble();
amountPaid = Double.parseDouble(PaidString);
//Calculate Tax
double TaxableIncome = grossIncome - deduction1 - deduction2 - deduction3;
double TaxObligation = TaxableIncome * taxrate;
double NetIncome = TaxableIncome - TaxObligation;
double TaxesDue = TaxObligation - amountPaid;
//Displays the results
String output = "Total Income :" + grossIncome + " Deduction for Children :" + deduction1 + " Deduction for Age :" + deduction2 + " Deduction for Marital Status: " + deduction3 + " Taxable Income: " + TaxableIncome + " Tax Obligation: " + TaxObligation + " Net Income: " + NetIncome + " Taxes Paid: " + amountPaid + " Taxes Paid: " + amountPaid + " Taxes Due: " + TaxesDue;
JOptionPane.showMessageDialog(null, output);
}
}
Explanation / Answer
Please rate...
This is the rectified program:
===================================================================
import java.util.Scanner;
import javax.swing.JOptionPane;
public class TaxGUI {
public static void main(String[] args) {
final double Dependant_Deduction = 2000.00;
final double Senior_Deduction = 0.50;
final double Minor_Deduction = 0.25;
final double Single_Deduction = 500.0;
final double Married_Deduction = 750.0;
Scanner reader = new Scanner(System.in);
//Variables
double grossIncome;//User's input
int numDependant;//User's input
int age; //User's input
int maritalStatus; // User's input
double amountPaid; //User's input
//Request input from the user
//Calculate Tax Rate
String grossIncomeString = JOptionPane.showInputDialog("Enter your gross income: ");
grossIncome = Double.parseDouble(grossIncomeString);
double taxrate;
if (grossIncome <= 10000)
{
taxrate = 0.18;
}
else if (grossIncome <= 50000)
{
taxrate = 0.31;
}
else
{
taxrate = 0.40;
}
//Request input from the user
//Calculate Deduction for children
String numDependantString = JOptionPane.showInputDialog("Enter your dependants: ");
numDependant = Integer.parseInt(numDependantString);
double deduction1 = numDependant * Dependant_Deduction;
//Request input from the user
//Calculate Deduction for age
String ageString = JOptionPane.showInputDialog("Enter your age: ");
age = Integer.parseInt(ageString);
double deduction2;
if(age >= 65)
{
deduction2 = (grossIncome - deduction1) * Senior_Deduction;
}
else if(age <= 17)
{
deduction2 = (grossIncome - deduction1) * Minor_Deduction;
}
else
{
deduction2 = 0;
}
//Request input from the user
//Calculate Deduction for martial status
String maritalStatusString = JOptionPane.showInputDialog("Enter your marital status(1=single 2=married): ");
maritalStatus = Integer.parseInt(maritalStatusString);
double deduction3=0;
if(maritalStatus == 1)
{
deduction3 = Single_Deduction;
}
else if(maritalStatus == 2)
{
deduction3 = Married_Deduction;
}
//Request input from the user for the tax paid
String amountPaidString = JOptionPane.showInputDialog("Enter the amount you paid in taxes during the year: ");
amountPaid = Double.parseDouble(amountPaidString);
//Calculate Tax
double TaxableIncome = grossIncome - deduction1 - deduction2 - deduction3;
double TaxObligation = TaxableIncome * taxrate;
double NetIncome = TaxableIncome - TaxObligation;
double TaxesDue = TaxObligation - amountPaid;
//Displays the results
String output = "Total Income :" + grossIncome + " Deduction for Children :" + deduction1 + " Deduction for Age :" + deduction2 + " Deduction for Marital Status: " + deduction3 + " Taxable Income: " + TaxableIncome + " Tax Obligation: " + TaxObligation + " Net Income: " + NetIncome + " Taxes Paid: " + amountPaid + " Taxes Due: " + TaxesDue;
JOptionPane.showMessageDialog(null,output);
}
}