• Post Reply Bookmark Topic Watch Topic
  • New Topic

Scanner Objects  RSS feed

 
Mark Captain
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am supposed to write methods that take a single Scanner object as a parameter in order to read two different .txt files. I can think of other ways to do what I need to but I can't figure out how to do this required way.

So I created my Scanner object here




and need to set it as a parameter, I have so far




Not sure how to go about this. Thoughts?
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You said you could think of other ways to do this, I'm assuming that means reading from a file on the local filesystem.
If you look at the Scanner API documentation, more specifically the constructor summary, you will probably notice a few familiar classes there.
For instance, a very convenient way to construct a new Scanner is by use the constructor that take a File instance. Simply call that and you're Scanner is good to go to start reading input from the File you specified.
As for the implementation of the methods, it really depends on the contents of the file and how you want to proces it.
A good starting point might be this Scanner tutorial, or of course the Scanner's API documentation.

[Edit]Refine the API link[/edit]
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you actually read two files with one Scanner object? I thought you need one instance for each file. Please check whether I am mistaken.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope, you'd need two distinct Scanner instances.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!