I’m new to Java. I’m taking an online class this semester. The book which we are using isn’t the greatest. I’ve tried searching around for answers online, but I’m coming up short. Maybe one of you guys can help me out 🙂
Basically the program below is suppose to prompt the user to enter info, the program then calculates the deductions and outputs the info to a .txt file. It runs and complies without error. The dialog boxes pop up, but no data is being written to the file. The .txt file is saved in the same folder btw. Any ideas?
import java.io.*;
import java.util.*;
import javax.swing.JOptionPane;
public class PaycheckCalculator
{
static final double FEDERAL_TAX = .15, STATE_TAX = .035, SOCIAL_SECURITY = .0575, MEDICARE_MEDICAID = .0275,PENSION = .05;
static final int HEALTH_INSURANCE = 75;
public static void main (String[] args) throws FileNotFoundException
{
String employeeName;
String inputStr;
double grossPay, netPay, federal, state, social, medicare, pension;
int healthInsurance;
PrintWriter outFile = new PrintWriter ("paycalc.txt");
employeeName = JOptionPane.showInputDialog ("Please enter your name: ");
inputStr = JOptionPane.showInputDialog ("Please enter your monthly gross pay: ");
grossPay = Double.parseDouble (inputStr);
federal = (grossPay * FEDERAL_TAX);
state = (grossPay * STATE_TAX);
social = (grossPay * SOCIAL_SECURITY);
medicare = (grossPay * MEDICARE_MEDICAID);
pension = (grossPay * PENSION);
netPay = (grossPay - federal - state - social - medicare - pension - HEALTH_INSURANCE);
outFile.println (employeeName);
outFile.printf ("Gross Amount: %.2f%n", grossPay);
outFile.printf ("Federal Tax: %.2f%n", federal);
outFile.printf ("State Tax: %.2f%n", state);
outFile.printf ("Social Security Tax: %.2f%n", social);
outFile.printf ("Medicare\\Medicaid Tax: %.2f%n", medicare);
outFile.printf ("Pension Plan: %.2f%n", pension);
outFile.printf ("Health Insurance: $" + HEALTH_INSURANCE);
outFile.printf ("Net Pay: %.2f%n", netPay);
System.exit(0);
outFile.close();
}
}
You have to close your
PrintWriterbefore your application exits.You can either remove the
System.exit(0);line (which is useless) or put it after theclose()operation.This way the data will be flushed into your paycalc.txt file before the application exits.