我是Java的新手。我这个学期正在上网。我们使用的这本书并不是最好的。我试着在网上寻找答案,但我很快就会出现问题。也许你们中的一个人可以帮助我:)
基本上,下面的程序假定提示用户输入信息,程序然后计算扣除并将信息输出到.txt文件。它运行并遵守,没有错误。弹出对话框,但没有数据写入文件。 .txt文件保存在同一个文件夹中。有什么想法吗?
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();
}
}
答案 0 :(得分:2)
您必须在申请退出前关闭PrintWriter
。
您可以删除System.exit(0);
行(无用)或在close()
操作后放置。
这样,在应用程序退出之前,数据将被刷新到paycalc.txt文件中。
答案 1 :(得分:1)
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: ");