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

Thread: FileNotFoundException via Scanner

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default FileNotFoundException via Scanner

    Hello,

    This block of code:

    public Member parseFile(String file_loc) throws FileNotFoundException{
    File memberFile = new File(file_loc);
    LinkedList stackList = new LinkedList();
    Scanner s = new Scanner(memberFile);
    while (s.hasNextLine()){
    stackList.addLast(s.nextLine());
    }

    Produce this error:

    java.io.FileNotFoundException: PA1-exampleData (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.util.Scanner.<init>(Unknown Source)
    at a1.InformationParser.parseFile(InformationParser.j ava:37)

    PA1-exampleData is a file that is passed to this procedure (within the class InformationParser) through the main of another class.

    I am executing this is the same directory of the file yet the error indicates that the it cannot find the file.

    This error occurs on line 37 of InformationParser, at the line reading " Scanner s = new Scanner(memberFile); ".

    Am I missing something here?


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: FileNotFoundException via Scanner

    You can use File.getAbsolutePath() to get the full path the JVM will use to locate the file. Print the absolute path out just before the error happens. Is it what you expect?

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: FileNotFoundException via Scanner

    Adding System.out.println(memberFile.getAbsolutePath()); before the error causes it to display the same error message, but no path is printed before it occurs. Odd.

  4. #4
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: FileNotFoundException via Scanner

    Did you check the line numbers in the Exception? If you added a line and compiled your code the line numbers should at least have increased by 1.

  5. The Following User Says Thank You to Sean4u For This Useful Post:

    mtn_student (August 31st, 2011)

  6. #5
    Junior Member
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: FileNotFoundException via Scanner

    Yes, the error now happens on line 38 instead of line 37.

  7. #6
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: FileNotFoundException via Scanner

    Maybe you should change that line to:
    System.out.println("****\n\n\n****LOOKIT THIS FILE!!!! '" + memberFile.getAbsolutePath() + "' ****\n\n\n\n    **** CAN YOU SEE ME NOW?????\n\n\n\n****");
    There's always a chance that getAbsolutePath() is returning something very short.

  8. #7
    Junior Member
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: FileNotFoundException via Scanner

    Nevermind, I just found out that I'm in the wrong directory. Thumbsup for Sean4u anyway

Similar Threads

  1. Scanner help
    By sp11k3t3ht3rd in forum Java Theory & Questions
    Replies: 3
    Last Post: June 22nd, 2011, 11:55 AM
  2. Console box with scanner?
    By Hallowed in forum Java Theory & Questions
    Replies: 1
    Last Post: May 26th, 2011, 12:50 AM
  3. FileNotFoundException!?
    By Shaddam in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: June 29th, 2010, 06:46 PM
  4. How to take a char value in scanner
    By andaji in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 1st, 2010, 02:50 AM
  5. Help With Scanner
    By jtphenom in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 12th, 2009, 08:49 PM