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 11 of 11

Thread: Can someone explain why the FILENOTFOUNDEXCEPTION keeps beign thrown?

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can someone explain why the FILENOTFOUNDEXCEPTION keeps beign thrown?

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    public class TreeDriver {
     
    	public static void main(String [] args) {
     
    		Scanner input = new Scanner(System.in);
     
    		Tree test = new Tree();
     
    	String label ;
     
    		String prompt;
     
    		String message;
     
    		String filename = "sample.txt";
     
     
     
     
    		Tree family = new Tree();
     
     
    				try {
     
    					input= new Scanner(new File(filename));  
     
    					while(input.hasNextLine()){
     
    					label = input.nextLine();
     
    					prompt = input.nextLine();
     
    					message = input.nextLine();
     
    					family.root = new TreeNode(label,prompt,message);	
     
    					family.addNode(label, prompt, message, label);
     
    					}
     
    					input.close();
     
    					} catch (FileNotFoundException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
     
     
     
     
    	}
    	}

    I made sure the file and the class are in the same project but no matter what I do, it keeps throwing that exception. Please help I as soon as you can b/c this is part of an hw assignment


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: Can someone explain why the FILENOTFOUNDEXCEPTION keeps beign thrown?

    Isn't the exception self-explanatory? The JVM cannot find the file. Either you have misspelled the name or the file is not in the correct location (most likely reason). Try using an absolute path instead or a relative one.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can someone explain why the FILENOTFOUNDEXCEPTION keeps beign thrown?

    I didn't misspell it. So how do I put the file in its correct location?

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: Can someone explain why the FILENOTFOUNDEXCEPTION keeps beign thrown?

    Did you fail to read the last sentence?
    Improving the world one idiot at a time!

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can someone explain why the FILENOTFOUNDEXCEPTION keeps beign thrown?

    I tried you suggestions and although I didn't run into any exceptions, when I tried to print info from the file, I received info that I didn't even put on the file. Can I get help resolving this issue?

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: Can someone explain why the FILENOTFOUNDEXCEPTION keeps beign thrown?

    I cannot see you screen therefore I have no idea what your code looks like, what your file looks like, where your file is located. Basically I am relying on information provided by you which is next to nothing. How do you expect anyone to help?
    Improving the world one idiot at a time!

  7. #7
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: Can someone explain why the FILENOTFOUNDEXCEPTION keeps beign thrown?

    Put the file in the same folder as the class source file

  8. #8
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Can someone explain why the FILENOTFOUNDEXCEPTION keeps beign thrown?

    Off Topic: That while loop looks unsafe. Verify a line exists, then attempt to take three...

  9. #9
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Can someone explain why the FILENOTFOUNDEXCEPTION keeps beign thrown?

    cbplayer,
    can you post the contents of "sample.txt" file and the screen output you have got?

    Syed.

  10. #10
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: Can someone explain why the FILENOTFOUNDEXCEPTION keeps beign thrown?

    Quote Originally Posted by llowe29 View Post
    Put the file in the same folder as the class source file
    That might not always work. The JVM will look for the file relative to the working directory and not relative to the class file. The working directory may be different to the location of the class file. This is why I suggested using an absolute file path.
    Improving the world one idiot at a time!

  11. #11
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Can someone explain why the FILENOTFOUNDEXCEPTION keeps beign thrown?

    Hello.
    Are you using any IDE? Or are you doing it on the console?

    Syed.

Similar Threads

  1. Which of the subclasses of IOException are more probably to be thrown?
    By Kate123 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 16th, 2013, 07:25 AM
  2. Exception Thrown
    By Frame in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 30th, 2011, 12:14 AM
  3. caught or declare might be thrown error
    By IDK12 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 28th, 2011, 01:55 PM
  4. Replies: 1
    Last Post: December 13th, 2010, 05:13 AM
  5. FileNotFoundException!?
    By Shaddam in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: June 29th, 2010, 07:46 PM