• 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

Extracting strings from files?

 
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If i have a text file with each line containing two bits of information about rooms, such as:



whereby the first bit of information refers to the room name which should be stored in a String array called roomNames and the second bit refers to the capacity of the rooms which should be stored in a String array called
roomCapacity, how do i seperate the two so that this can happen?

i was thinking tokenizers... but dont know how to use them to do this
cheers
[ February 02, 2006: Message edited by: Sam Bluesman ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sam,

In JDK 1.4 and later, the String.split() method is easier to use than the StringTokenizer class.

String line = "Room1, 120";
String[] data = line.split("[ \t,]+");

The argument to split() is a regular expression, which scares some people, but they're really not so bad. The expression I've shown will break tokens at any combination of space, tab, and comma characters.
 
Sam Bluesman
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. That worked a treat!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic