• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Scanner Objects  RSS feed

 
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?
 
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]
 
Marshal
Posts: 60759
189
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!