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
Quote:
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:
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?
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.
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...?
Re: Scanner malfunction - Reading a file
Scanner throws a FileNotFoundException, so you have to catch it in a try/catch-statement.
Re: Scanner malfunction - Reading a file
Quote:
Originally Posted by
Noceo
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?
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
Re: Scanner malfunction - Reading a file
...
Quote:
Originally Posted by
JBelg
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.
Re: Scanner malfunction - Reading a file
Quote:
Originally Posted by
Noceo
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.
Re: Scanner malfunction - Reading a file
Mkay, I added the following to the code:
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).
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.
Re: Scanner malfunction - Reading a file
Thank you very much everyone. I think I might have just learned something :(nod):