Results 1 to 6 of 6
  1. #1
    Dex
    Dex is offline Member
    Join Date
    Mar 2012
    Posts
    29
    Rep Power
    0

    Default Scanner FileNotFoundException

    Hi I'm new to scanners and i'm getting this error:

    Java Code:
    Error: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
    I am trying to read a text file and I think the problem might be the way i'm creating my scanner, but i'm not sure..

    Java Code:
    String fileName = "Save.txt";
        Scanner scan = null;
          
        File file = new File(fileName);  
        scan = new Scanner(file);
    Have i done something wrong? I have also tried using an absolute path, but i got the same error.. :(
    Thanks.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default Re: Scanner FileNotFoundException

    The compiler doesn't know anything about paths, whether absolute or not. All it knows is that possibly a file could not be found and the Scanner might throw an Exception and your code doesn't anticipate on it, nor does it state that it lets pass the Exception; either catch the Exception with a try ... catch block or tell the compiler that your method lets this Exception pass ('throws' in the header in of the method).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Dex
    Dex is offline Member
    Join Date
    Mar 2012
    Posts
    29
    Rep Power
    0

    Default Re: Scanner FileNotFoundException

    Quote Originally Posted by JosAH View Post
    The compiler doesn't know anything about paths, whether absolute or not. All it knows is that possibly a file could not be found and the Scanner might throw an Exception and your code doesn't anticipate on it, nor does it state that it lets pass the Exception; either catch the Exception with a try ... catch block or tell the compiler that your method lets this Exception pass ('throws' in the header in of the method).

    kind regards,

    Jos
    Hi Jos,

    thanks for the info, but my problem is that I know that the txt file exists in my directory but my scanner cant find it for some reason.
    Regards
    Dex

  4. #4
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: Scanner FileNotFoundException

    Quote Originally Posted by Dex View Post
    Hi Jos,

    thanks for the info, but my problem is that I know that the txt file exists in my directory but my scanner cant find it for some reason.
    Regards
    Dex
    There is one problem, you know that your .txt-file exist in your directory, but does your Scanner know this?

    Is it in your current working directory?

    You can also try to put a dot before your file.txt-path, like

    Java Code:
    String fileName = "./Save.txt";
        Scanner scan = null;
           
        File file = new File(fileName);  
        scan = new Scanner(file);

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Scanner FileNotFoundException

    Quote Originally Posted by Dex View Post
    Hi Jos,

    thanks for the info, but my problem is that I know that the txt file exists in my directory but my scanner cant find it for some reason.
    Regards
    Dex
    You need to handle the exception.
    The Scanner(File) constructor throws a FileNotFoundException, so you have to handle it in your code, as Jos says.

    Here's the exceptions tutorial.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Dex
    Dex is offline Member
    Join Date
    Mar 2012
    Posts
    29
    Rep Power
    0

    Default Re: Scanner FileNotFoundException

    Quote Originally Posted by Tolls View Post
    You need to handle the exception.
    The Scanner(File) constructor throws a FileNotFoundException, so you have to handle it in your code, as Jos says.

    Here's the exceptions tutorial.
    Yes thank you for all of the information, I needed to use try & catch. :)

Similar Threads

  1. Help | FileNotFoundException: db.properties
    By Maxpain43 in forum NetBeans
    Replies: 0
    Last Post: 07-22-2011, 10:35 AM
  2. Help with FileNotFoundException
    By Beginner in forum New To Java
    Replies: 11
    Last Post: 11-22-2010, 04:44 PM
  3. FileNotFoundException
    By ProgrammingPup in forum Advanced Java
    Replies: 4
    Last Post: 12-30-2009, 01:29 AM
  4. filenotfoundexception :@
    By wildheart in forum New To Java
    Replies: 2
    Last Post: 04-25-2009, 09:56 AM
  5. FileNotFoundException
    By PeonLover in forum New To Java
    Replies: 1
    Last Post: 12-26-2007, 06:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •