2010-10-09 68 views
1

我是Java新手。我在本學期參加在線課程。我們正在使用的書不是最偉大的。我試着在網上搜索答案,但我很快就會發現。也許你們中的一個人可以幫我:)帶文件輸出的JOptionPane

基本上下面的程序是假設提示用戶輸入信息,程序然後計算扣除並輸出信息到.txt文件。它運行並遵守,沒有錯誤。彈出對話框,但沒有數據正在寫入文件。 .txt文件保存在同一個文件夾btw中。有任何想法嗎?

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(); 

    } 
} 

回答

2

您必須在退出應用程序之前關閉PrintWriter

您可以刪除System.exit(0);行(無用)或將其放在close()操作之後。

這樣數據將在應用程序退出前刷新到您的paycalc.txt文件中。

+0

科林,非常感謝你!我非常感謝幫助。這樣一個簡單的小錯誤,但卻讓我發瘋。再次感謝你! – 2010-10-09 21:10:35

+0

System.exit並不總是完全沒用,有時Swing線程無法識別出沒有更多打開的窗口並繼續運行。其症狀是您的所有線程都已結束,但JVM不會終止。對於短期學習計劃無關緊要,但請稍後再將它放在腦後。 – Durandal 2010-10-09 21:51:08

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: "); 
+0

你能提供一些關於你所做更改的反饋嗎?此外,您的代碼似乎已被切斷。 – corsiKa 2012-11-16 04:57:44