This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Extracting strings from files? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Extracting strings from files?" Watch "Extracting strings from files?" New topic

Extracting strings from files?

Sam Bluesman
Ranch Hand

Joined: Nov 21, 2004
Posts: 191
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
[ February 02, 2006: Message edited by: Sam Bluesman ]

Moosey knows best
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24183

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.

[Jess in Action][AskingGoodQuestions]
Sam Bluesman
Ranch Hand

Joined: Nov 21, 2004
Posts: 191
Thanks. That worked a treat!
It is sorta covered in the JavaRanch Style Guide.
subject: Extracting strings from files?
Similar Threads
NX: customer ID in urlybird
Shoud I use window.location.replace() with Struts?
toString problem
How to create a method to accomodate array in method parameter?
multiple classes, objects and constructors