Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: File not found exception

  1. #1
    Member
    Join Date
    Aug 2011
    Posts
    55
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default File not found exception

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


  2. #2
    Member
    Join Date
    Sep 2011
    Location
    Nanuet, NY USA
    Posts
    33
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: File not found exception

    Quote Originally Posted by mwr76 View Post
    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);
     
     
    	}
    }
    You have to handle the exception. That is what the compiler is complaining about
    Last edited by kc120us; September 10th, 2011 at 01:05 PM.

  3. #3
    Member
    Join Date
    Aug 2011
    Posts
    55
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Re: File not found exception

    This is the thrown exception:

    Exception in thread "main" java.io.FileNotFoundException: C:\program.txt (Access is denied)
    at java.io.FileOutputStream.open(Native Method)

    We haven't gotten to handling exceptions. Does it have to be handled to figure out why its being thrown?

  4. #4
    Member
    Join Date
    Sep 2011
    Location
    Nanuet, NY USA
    Posts
    33
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: File not found exception

    Quote Originally Posted by mwr76 View Post
    This is the thrown exception:

    Exception in thread "main" java.io.FileNotFoundException: C:\program.txt (Access is denied)
    at java.io.FileOutputStream.open(Native Method)

    We haven't gotten to handling exceptions. Does it have to be handled to figure out why its being thrown?
    In your main method, you have to either throw the Exception or put your I/O statements in a try/catch block. Try looking here to figure out how to do it. Catching and Handling Exceptions (The Java™ Tutorials > Essential Classes > Exceptions)

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: File not found exception

    Quote Originally Posted by mwr76 View Post
    This is the thrown exception:

    Exception in thread "main" java.io.FileNotFoundException: C:\program.txt (Access is denied)
    at java.io.FileOutputStream.open(Native Method)

    We haven't gotten to handling exceptions. Does it have to be handled to figure out why its being thrown?
    The stack trace the exception prints (automatically if the exception is not caught, using the printStackTrace() method if you catch the exception) should have ample description...in your case an access denied based upon a FileNotFound. Make sure the file is in the correct location, and file name in the code is the same

    FYI, you should try and distinguish between a compile time versus runtime problem. Your original post concerned compile time, this latter case is runtime. Big difference.

Similar Threads

  1. Exception while reading xlsx file
    By tcstcs in forum Exceptions
    Replies: 1
    Last Post: May 5th, 2011, 05:06 AM
  2. no data found ms -access sql exception
    By jatinrai199 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 16th, 2011, 03:22 PM
  3. 500 exception while deploying a war file in tomcat
    By nrao in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 12th, 2011, 03:42 PM
  4. [SOLVED] File.createTempFile() throws exception on Win7
    By jitinsingla in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 10th, 2010, 02:14 PM
  5. exception while Read very large file > 300 MB
    By ps.ganesh in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: June 11th, 2009, 11:39 PM