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: 35224
7
posted
0
If you are certain that the names do not contain spaces you can use
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: 32611
4
posted
0
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
posted
0
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 = ___________; }
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.
Never ascribe to malice that which can be adequately explained by stupidity.
Jason Mackie
Greenhorn
Joined: Feb 07, 2007
Posts: 17
posted
0
sorry here is the code i just posted might be easier to read this way
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35224
7
posted
0
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
posted
0
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
posted
0
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."
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
posted
0
right on...thanks
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.