This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Help with some code for text read in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with some code for text read in java " Watch "Help with some code for text read in java " New topic
Author

Help with some code for text read in java

Jeff Heagle
Greenhorn

Joined: May 09, 2011
Posts: 3
Hi Everyone, I'm new to java and working on reading a text file into my java program. The issue with the code I have built is it only reads the text on each line and errors if there is more that one text on the line. Looking for some advice.

Here is the txt reader,


I need it to read 1 2 3 4 5, and let me choose one of them, right now I can only get it to read
1
2
3
4
5
from the text file. I believe it has something to do with my rates[i] = Double.parseDouble(line); but can't figure out how to correct it. Please help I'm not getting any help anywhere else.


Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2152
    
    7
Use String.split(...) to split the line of data into an array. Then you need to parse each entry in the array.
Jeff Heagle
Greenhorn

Joined: May 09, 2011
Posts: 3
Could you show an example? I've been trying to figure it out all day. Am I on the right track here?
Jeff Heagle
Greenhorn

Joined: May 09, 2011
Posts: 3
Got it now.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
Since Java5, most people would use a Scanner. Just beware of its nextLine method; it doesn't quite do what you think.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
. . . and welcome to the Ranch
 
Consider Paul's rocket mass heater.
 
subject: Help with some code for text read in java
 
Similar Threads
parse a csv file (PLEASE HELP)
NullPointerException
FileReader / BufferedReader will only read the first line from the file
Problem reading a file
cannot instantiate the the type Flower