File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Read integers from a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Read integers from a file" Watch "Read integers from a file" New topic

Read integers from a file

John Powell

Joined: Nov 13, 2004
Posts: 11
I'm trying to read integers from a file (2 integer values separated by whitespace) that the program will use as variables. The following code works correctly. I was wondering, is there an easier method to read integer values from a file and then assign the values to specific variables?

Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
That's pretty straightforward. If you're on JDK 1.4 or later, you can replace StringTokenizer with String.split() to tighten things up a bit. Test with some incorrect input, see what happens now and decide what you want to have happen:

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link:
subject: Read integers from a file
It's not a secret anymore!