• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reading from String

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

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 ]
 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
other alternative: look at StringTokenizer (java.util.StringTokenizer)
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
You can thank my dental hygienist for my untimely aliveness. So tiny:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic