I am using JGrasp. I am trying to write to a file. I am getting a file not found exception:
Monthly_pay.java:50: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
PrintWriter outFile = new PrintWriter("C:\\program.txt");
^
from what I read the book said that the file would not have to be there to output to the file. It said it would create it. So I don't understand
why the exception?
Here is my code:
import javax.swing.*; import java.io.*; public class Monthly_pay { static final double FED_TAX = .15; static final double STATE_TAX = .035; static final double SS_TAX = .0575; static final double MEDICARE = .0275; static final double PENSION = .05; static final double HEALTH_INS = 75.00; public static void main(String[] args) { String name, grossPay, outPut, dollarSign; double netPay, dGrossPay, fedTax, stateTax, ssTax, medicare, pension, healthIns; JOptionPane jop = new JOptionPane(); name = jop.showInputDialog("Please enter the employee name: "); grossPay = jop.showInputDialog("Please enter the employee's gross pay: "); dGrossPay = Double.parseDouble(grossPay); fedTax = dGrossPay * FED_TAX; stateTax = dGrossPay * STATE_TAX; ssTax = dGrossPay * SS_TAX; medicare = dGrossPay * MEDICARE; pension = dGrossPay * PENSION; netPay = dGrossPay - (fedTax + stateTax + ssTax + medicare + pension + HEALTH_INS); dollarSign = "$"; outPut = "Here is the break down of" + " " + name + "'s" + " " + "monthly pay. $" + "\n" + "Gross pay: $" + String.format("%.2f", dGrossPay) + "\n" + "Federal Tax: $" + String.format("%.2f", fedTax) + "\n" + "State Tax: $" + String.format("%.2f", stateTax) + "\n" + "Social Security Tax: $" + String.format("%.2f", ssTax) + "\n" + "Medicare / Medicade: $" + String.format("%.2f", medicare) + "\n" + "Pension Plan: $" + String.format("%.2f", pension) + "\n" + "Health Insurance: $" + String.format("%.2f", HEALTH_INS) + "\n" + "Net Pay: $" + String.format("%.2f", netPay); jop.showMessageDialog(null, outPut); PrintWriter outFile = new PrintWriter("C:\\program.txt"); outFile.printf(outPut); outFile.close(); System.exit(0); } }