
-----------------------------------
Sevenheartz
Sun Mar 01, 2009 4:05 pm

Getting file name using Scanner?
-----------------------------------
I've used Scanner in order to import a file and go through it [code]Scanner inFile = new Scanner(new File("Alice.txt"));[/code] but I don't know / can't find a command/method that would let me get the file name. What I want to do is get the file name so that I can output it (So in this case, when I run the program "Alice.txt" will be the output). Is this possible with scanner?

-----------------------------------
wtd
Mon Mar 02, 2009 1:16 pm

RE:Getting file name using Scanner?
-----------------------------------
No.  The Scanner class' constructor takes a Readable object and keeps track of it internally, but does not expose it via a method.

-----------------------------------
DemonWasp
Mon Mar 02, 2009 1:48 pm

RE:Getting file name using Scanner?
-----------------------------------
You can find documentation on all available methods of the core Java API here: http://java.sun.com/javase/6/docs/api/

You'll find that wtd is right; Scanner is a wrapper around any readable object (which may not necessarily have a file name associated with it - you can make a Scanner that reads along a String, for example); it does not expose either the wrapped object or any of its properties directly.

-----------------------------------
wtd
Mon Mar 02, 2009 1:53 pm

Re: RE:Getting file name using Scanner?
-----------------------------------
You'll find that wtd is right

You sound surprised.  ;-)

-----------------------------------
Sevenheartz
Mon Mar 02, 2009 3:16 pm

Re: RE:Getting file name using Scanner?
-----------------------------------
No.  The Scanner class' constructor takes a Readable object and keeps track of it internally, but does not expose it via a method.
You can find documentation on all available methods of the core Java API here: http://java.sun.com/javase/6/docs/api/

You'll find that wtd is right; Scanner is a wrapper around any readable object (which may not necessarily have a file name associated with it - you can make a Scanner that reads along a String, for example); it does not expose either the wrapped object or any of its properties directly.

Hmm, I guessed as much. In that case, may I ask how you all would obtain the file's name and output it?

-----------------------------------
DemonWasp
Mon Mar 02, 2009 3:24 pm

RE:Getting file name using Scanner?
-----------------------------------
Store it in another variable - either store the file name as a String, or else store the File object.

Edit: wtd, the surprise comes more from reading much of your epic rant against Java, then discovering that you're quite well-versed in it.

-----------------------------------
wtd
Mon Mar 02, 2009 3:46 pm

Re: RE:Getting file name using Scanner?
-----------------------------------
Store it in another variable - either store the file name as a String, or else store the File object.

Edit: wtd, the surprise comes more from reading much of your epic rant against Java, then discovering that you're quite well-versed in it.

My hatred comes from familiarity.  :-)

-----------------------------------
DemonWasp
Mon Mar 02, 2009 4:08 pm

Re: RE:Getting file name using Scanner?
-----------------------------------
My hatred comes from familiarity.  :-)

Funny, I get the same feeling about C++ - after using it for a while, it just looks like a collection of design errors and clever traps designed to snare the unwary.

But, we're side-tracking this thread. The answer to the original question is this:
- You can't retrieve the file name from a Scanner. Scanners do not necessarily scan files.
- You can keep track of the opened file separately if you're in a context where you happen to know that the Scanner IS actually scanning a file. Do this with a String.

-----------------------------------
Sevenheartz
Mon Mar 02, 2009 7:02 pm

Re: RE:Getting file name using Scanner?
-----------------------------------
My hatred comes from familiarity.  :-)

Funny, I get the same feeling about C++ - after using it for a while, it just looks like a collection of design errors and clever traps designed to snare the unwary.

But, we're side-tracking this thread. The answer to the original question is this:
- You can't retrieve the file name from a Scanner. Scanners do not necessarily scan files.
- You can keep track of the opened file separately if you're in a context where you happen to know that the Scanner IS actually scanning a file. Do this with a String.

Alright, I got it. Thanks for the help you two!

-----------------------------------
wtd
Mon Mar 02, 2009 7:11 pm

Re: RE:Getting file name using Scanner?
-----------------------------------
- You can't retrieve the file name from a Scanner. Scanners do not necessarily scan files.

THEORY WARNING!

This is what we call "polymorphism" in action.
