JOptionPane with file output
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.s开发者_JS百科wing.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 PrintWriter
before your application exits.
You can either remove the System.exit(0);
line (which is useless) or put it after the close()
operation.
This way the data will be flushed into your paycalc.txt file before the application exits.
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.FileOutputStream;
import java.io.File;
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)
{
String n = JOptionPane.showInputDialog("enter file name");
File fileObject = new File(n);
while (fileObject.exists())
{
JOptionPane.showMessageDialog(null,"the file "+fileObject+ "already exists ","Error dialog",JOptionPane.ERROR_MESSAGE);
JOptionPane.showInputDialog("enter file name again please");
String g = JOptionPane.showInputDialog("enter file name");
fileObject = new File(g);
}
PrintWriter outFile = null;
String employeeName = JOptionPane.showInputDialog("Enter your name");
String inputStr;
double grossPay, netPay, federal, state, social, medicare, pension;
int healthInsurance;
try
{
outFile = new PrintWriter(new FileOutputStream(fileObject));
}
catch(FileNotFoundException e)
{
System.out.println("this file could not be opened");
System.exit(0);
}
inputStr = JOptionPane.showInputDialog ("Please enter your monthly gross pay: ");
精彩评论