This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I have a program that reads lines from a file. I save the lines to the String rivi. The problem is that one line contains 2 short Strings, one double and one integer. I need them separately, so how do separate them from that String?
For example, I have a String longString that contains the following data: sukunimi etunimi 12345 52.415
I would like to save them in separate variables: String shortString1 = sukunimi String shortString2 = etunimi int number1 = 12345 double number2 = 52.415
Take a look at the split method in java.lang.String. This will split your String into an array of separate String objects, which you can then work with (e.g., parse as primitive values). [ April 11, 2006: Message edited by: marc weber ]
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
And when you're looking at the API doc for StringTokenizer, note the sentence
StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
This may get us tossed out of the beginner forum, but if you get all those options down cold, peek at the JavaDoc for Scanner. It can read from a file and parse the lines into variables of the proper type all at once. If it looks too advanced, stick with split() and just file the name away for another day.
Let us know how you work this out!
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