Win a copy of Microservices in Action this week in the Web Services forum!
  • 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
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

another i/o question  RSS feed

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Rancher
Posts: 42974
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are certain that the names do not contain spaces you can use

String[] pieces = line.split(" ");
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could get the whole line and then split it instead of getting each elements. Hope this helps.


 
Marshal
Posts: 61805
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
lowercase baba
Posts: 12628
50
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jason Mackie
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry here is the code i just posted might be easier to read this way

 
Ulf Dittmer
Rancher
Posts: 42974
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jason Mackie
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
right on...thanks
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!