Results 1 to 11 of 11
  1. #1
    Noceo is offline Member
    Join Date
    Oct 2012
    Posts
    9
    Rep Power
    0

    Unhappy Scanner malfunction - Reading a file

    Hey there,

    I'm trying to get a scanner to read a file, but it keepøs giving the error

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    Unhandled exception type FileNotFoundException
    I tried both reading just using the filename (which is in the same directory as my .java file) and also to read it with the full path. My code is as following:

    Java Code:
    import java.io.*;
    import java.util.*;
    
    
    public class simpleRead {
    	
    	
    	
    	public static void main(String args[]) {
    		
    		File fileName 	= new File("C:/Users/Jakob/Dropbox/DTU/7_Semester/02101 Indledende Programmering/Eclipse Projects/Assignment 2/src/simpletext01.txt");
    		
    		
    		if (fileName.exists() && fileName.length() > 0) {
    			Scanner inFile 	= new Scanner(fileName);
    			System.out.println(fileName.length());
    		}else{
    			System.out.println("Smth went wrong");
    		}
    	
    	}
    
    }
    What the duck am I doing wrong here? :-(


    P.S.: Why does my code show up with an additional 10-ish empty lines in here?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default Re: Scanner malfunction - Reading a file

    Well, that error implies it doesn't compile.
    "Unresolved compilation problem".
    So solve that first.
    You can't run something that doesn't compile.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Noceo is offline Member
    Join Date
    Oct 2012
    Posts
    9
    Rep Power
    0

    Default Re: Scanner malfunction - Reading a file

    This is probably a stupid question, but how do I find out why it does not compile? The code works fine if I comment out the scanner line...?

  4. #4
    JBelg is offline Member
    Join Date
    Oct 2012
    Posts
    36
    Rep Power
    0

    Default Re: Scanner malfunction - Reading a file

    Scanner throws a FileNotFoundException, so you have to catch it in a try/catch-statement.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default Re: Scanner malfunction - Reading a file

    Quote Originally Posted by Noceo View Post
    This is probably a stupid question, but how do I find out why it does not compile? The code works fine if I comment out the scanner line...?
    How can you possibly write code without knowing how to identify compiler errors?
    How are you writing this?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Noceo is offline Member
    Join Date
    Oct 2012
    Posts
    9
    Rep Power
    0

    Default Re: Scanner malfunction - Reading a file

    I'm using Eclipse. And I only just started learning Java, and also apparently i suck at it :s

  7. #7
    JBelg is offline Member
    Join Date
    Oct 2012
    Posts
    36
    Rep Power
    0

    Default Re: Scanner malfunction - Reading a file

    ...
    Quote Originally Posted by JBelg View Post
    Scanner throws a FileNotFoundException, so you have to catch it in a try/catch-statement.
    You have to either catch to exception or keep throwing it, but in this case you'll want to catch it with a try/catch. This is not an option, you have to either catch it or throw it.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default Re: Scanner malfunction - Reading a file

    Quote Originally Posted by Noceo View Post
    I'm using Eclipse. And I only just started learning Java, and also apparently i suck at it :s
    No, you just don't understand the IDE you;re using.
    I, personally, wouldn't recommend an IDE if you're just starting out.

    Anyway, Eclipse underlines any problem lines in red, and sticks a little error symbol in the gutter.
    Hovering over either will give you the error text.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    Noceo is offline Member
    Join Date
    Oct 2012
    Posts
    9
    Rep Power
    0

    Default Re: Scanner malfunction - Reading a file

    Mkay, I added the following to the code:

    Java Code:
    public static void main(String args[]) throws FileNotFoundException {
    And then it seems to work. I really don't get why it doesn't work without it. Is it because it needs to have exception handling, in order to compile? (In case the file doesn't excist).

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default Re: Scanner malfunction - Reading a file

    Yes.
    That's it exactly.
    The constructor for Scanner can throw that exception, so you have to handle it.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    Noceo is offline Member
    Join Date
    Oct 2012
    Posts
    9
    Rep Power
    0

    Default Re: Scanner malfunction - Reading a file

    Thank you very much everyone. I think I might have just learned something

Similar Threads

  1. Reading a file with Scanner
    By Games2Design in forum New To Java
    Replies: 5
    Last Post: 10-04-2012, 10:10 AM
  2. File reading with Scanner
    By mik in forum New To Java
    Replies: 2
    Last Post: 08-31-2012, 09:44 AM
  3. Scanner not reading every character in a file?
    By Brandonhspace in forum New To Java
    Replies: 5
    Last Post: 06-07-2012, 07:57 AM
  4. Reading file problem using Scanner
    By nfsmwbe in forum New To Java
    Replies: 18
    Last Post: 01-04-2012, 03:26 PM
  5. Reading pattern using Scanner class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-18-2008, 12:02 PM

Posting Permissions

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