Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes another i/o question 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 "another i/o question" Watch "another i/o question" New topic
Author

another i/o question

Jason Mackie
Greenhorn

Joined: Feb 07, 2007
Posts: 17
if i have to input 5 separate lines of code like:

Smith 27 83.7
Jones 21 28.35
Walker 96 182.4
Doe 60 150
Wood 100 400

how would i be able to put smith, 27, and 83.7 in separate variables?

here is my code so far...



there is more at the bottom but that is not important! I just need some guidance as to how to read a line of code from a file and then put the 3 different variables in their corresponding variables (im looking for help with the last comment in the snippet of code)
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41068
    
  43
If you are certain that the names do not contain spaces you can use

String[] pieces = line.split(" ");


Ping & DNS - my free Android networking tools app
Srikanth Ramu
Ranch Hand

Joined: Feb 20, 2007
Posts: 76
You could get the whole line and then split it instead of getting each elements. Hope this helps.


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37936
    
  22
If you are using a Scanner, it has methods which allow you to get the next int, the next double or the next token. Have a look in its API documentation here.

CR
Jason Mackie
Greenhorn

Joined: Feb 07, 2007
Posts: 17
it seems to me that the whole "split" thing would work but how would i be able to store them in differnt variables?

in the file im inputing there are 3 "variables" per line. the first one is name which has no spaces, the second is an integer that is called creditHrs, and the last is a double called qualityPts. I was thinking of using an if statement like

if (______ == string or char){
name == _________;
}
else if (_______ == int){
creditHrs = _____________;
}
else if (_______ == double){
qualityPts = ___________;
}

is there any way to do that?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

the API tells you that the split method returns an array of strings.

You know the [0]th element will have first token, or the name. the [1]st element will have the second token, or the int. the [2]nd element will have the double. write a statement for each that assigns or (parses and assigns) them to the variables you want.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jason Mackie
Greenhorn

Joined: Feb 07, 2007
Posts: 17
sorry here is the code i just posted might be easier to read this way

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41068
    
  43
How about something like the following:

String[] pieces = line.split(" ");
String name = pieces[0];
int creditHrs = Integer.parseInt(pieces[1]);
double qualityPts = new Double(pieces[2]).doubleValue();

You really need to be sure that the names do not contain spaces, though.
Jason Mackie
Greenhorn

Joined: Feb 07, 2007
Posts: 17
i actually used the StringTokenizer to solve the problem...I tried messing around with "split" for a long while and was not able to come to a solution. Here was my finishing code:



thanks to everyone that helped...
Jason
David McCombs
Ranch Hand

Joined: Oct 17, 2006
Posts: 212
Don't use StringTokenizer. For one thing String.split() is far simpler, and StringTokenizer is a legacy class, only kept around to not break older code.

From the StringTokenizer description in the API: "StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code."

http://java.sun.com/j2se/1.5.0/docs/api/index.html

Make sure you have the right regular expression as an argument in split, Passing " " as an argument to split should work just fine, unless the data is not in the format you expect.


"Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration."- Stan Kelly-Bootle
Jason Mackie
Greenhorn

Joined: Feb 07, 2007
Posts: 17
right on...thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: another i/o question
 
Similar Threads
float
adding all the numbers in a string
NullPointerExcpetion Help
when i run my virtual machine just keeps going, but no output.
Help. Recognising white space in a string