Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Getting file name using Scanner?
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Sevenheartz




PostPosted: Sun Mar 01, 2009 4:05 pm   Post subject: 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"));
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?
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Mon Mar 02, 2009 1:16 pm   Post subject: 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




PostPosted: Mon Mar 02, 2009 1:48 pm   Post subject: 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




PostPosted: Mon Mar 02, 2009 1:53 pm   Post subject: Re: RE:Getting file name using Scanner?

DemonWasp @ Tue Mar 03, 2009 2:48 am wrote:
You'll find that wtd is right


You sound surprised. Wink
Sevenheartz




PostPosted: Mon Mar 02, 2009 3:16 pm   Post subject: Re: RE:Getting file name using Scanner?

wtd @ Mon Mar 02, 2009 1:16 pm wrote:
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 wrote:
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




PostPosted: Mon Mar 02, 2009 3:24 pm   Post subject: 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




PostPosted: Mon Mar 02, 2009 3:46 pm   Post subject: Re: RE:Getting file name using Scanner?

DemonWasp @ Tue Mar 03, 2009 4:24 am wrote:
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. Smile
DemonWasp




PostPosted: Mon Mar 02, 2009 4:08 pm   Post subject: Re: RE:Getting file name using Scanner?

wtd @ Mon Mar 02, 2009 3:46 pm wrote:
My hatred comes from familiarity. Smile


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.
Sponsor
Sponsor
Sponsor
sponsor
Sevenheartz




PostPosted: Mon Mar 02, 2009 7:02 pm   Post subject: Re: RE:Getting file name using Scanner?

DemonWasp @ Mon Mar 02, 2009 4:08 pm wrote:
wtd @ Mon Mar 02, 2009 3:46 pm wrote:
My hatred comes from familiarity. Smile


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




PostPosted: Mon Mar 02, 2009 7:11 pm   Post subject: Re: RE:Getting file name using Scanner?

DemonWasp @ Tue Mar 03, 2009 5:08 am wrote:
- 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.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: