Two questions: Scanner and exception handlers

Extreme Newbie Alert: I took a single college semester involving Java, almost five years ago, and haven't really touched it since until recently when I decided to try to get back into it. I am now working on building a program while reading the Java 5.0 Tutorial downloadable from Sun.

Okay, now that the faint of heart have left us, here are the two questions I want to ask:

1) I am trying to use the Scanner tool ( http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html ) on a FileReader wrapped in a BufferedReader. I have installed JDK 5.0 and I am importing java.util.*, but when I try to compile I get this error:

Wishlist.java:71: cannot resolve symbol

symbol : class Scanner

location: class Wishlist

Scanner readFromMe = new Scanner(new BufferedReader(new FileReader("wishlist.txt")));

^

...and then another one pointing to the other appearance of "Scanner" in the same line, followed by six more in places where I use the readFromMe variable that line was supposed to declare and initialize. Any idea why the compiler doesn't recognize Scanner even though I'm importing its package?

2) Is there anything preventing you from nesting exception handlers? As you've seen, in this program I open a file...if that attempt produces a FileNotFoundException, I want the error handling to create a new, empty file to use in place of the one that should have been there. Since this could throw an IOException of its own, I would need to put a try | catch block into the other catch block. Is the compiler going to stand for that? Alternatively, is there any input stream that creates a file with the specified filename if there is none already? Probably not, since a newly created file will be empty and there's no point in having an input stream on it...

[1834 byte] By [Platonixa] at [2007-10-3 2:53:20]
# 1

1) Are you sure you installed and are invoking jdk 1.5? Maybe you have an earlier version ahead of it on your path.

2) yeah you can nest try/catch blocks like that. However, in this case, exceptions sound like the wrong way to go. A missing file sounds like a real, regular possibility -- it's not that exceptional as you describe it. A better thing would be to create a File object, see if the file exists, and then if it doesn't go ahead and immediately handle that case. Don't even try to open the file that may not exist.

paulcwa at 2007-7-14 20:42:20 > top of Java-index,Java Essentials,New To Java...
# 2

> 1) Are you sure you installed and are invoking jdk

> 1.5? Maybe you have an earlier version ahead of it

> on your path.

Hm. I did use 1.3 back in college...I checked the contents of PATH and the User Variables version of it was set correctly but the System Variables version of it was set to 1.3. I corrected the System Variables version of it and now the compiler doesn't complain about Scanner...but I'm still getting errors that it doesn't recognize readFromMe even after I initialized it. Odd...

> 2) yeah you can nest try/catch blocks like that.

> However, in this case, exceptions sound like the

> wrong way to go. A missing file sounds like a real,

> regular possibility -- it's not that exceptional as

> you describe it. A better thing would be to create

> a File object, see if the file exists, and then if

> it doesn't go ahead and immediately handle that

> case. Don't even try to open the file that may not

> exist.

Okay, I'll try rewriting the file read like that, and hopefully the readFromMe errors will go away. If they don't I'll make a new post...probably with the entirety of the code because I can't even guess at what part of the code is causing that. Thanks for the help!

Platonixa at 2007-7-14 20:42:20 > top of Java-index,Java Essentials,New To Java...
# 3

> .but I'm still getting errors that it

> doesn't recognize readFromMe even after I initialized

> it. Odd...

Just a guess -- did you declare readFromMe in a try/catch block, but then reference it outside the block?

> Okay, I'll try rewriting the file read like that, and

> hopefully the readFromMe errors will go away.

I don't think it would make those errors go away. It's a completely different thing. It's just a cleaner design.

paulcwa at 2007-7-14 20:42:20 > top of Java-index,Java Essentials,New To Java...
# 4

> > .but I'm still getting errors that it

> > doesn't recognize readFromMe even after I

> initialized

> > it. Odd...

>

> Just a guess -- did you declare readFromMe in a

> try/catch block, but then reference it outside

> the block?

Agh! You're exactly right, it's a scope problem! Somehow I didn't think that applied to error handling...so I need to declare readFromMe outside the try block and then initialize it inside the try block...or just put the entirety of the file-reading code inside that try block. I'll have to study the different types of IOExceptions so I'll still be able to tell whether there's a problem preventing me from reading the file or a problem with the file's formatting.

Platonixa at 2007-7-14 20:42:20 > top of Java-index,Java Essentials,New To Java...