File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Parsing out text in a predictable pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Parsing out text in a predictable pattern" Watch "Parsing out text in a predictable pattern" New topic

Parsing out text in a predictable pattern

Terry Chambers

Joined: May 11, 2009
Posts: 17
I have a string of text:

word 1 1 4.0 text 1 2 3.5 dog 3 4 1.0 walker 3 2 4.0

The pattern is:

|<word> <number> <number> <number.number>| |<word> <number> <number> <number.number>| |<word> <number> <number> <number.number>|

I want to be able to take this string of text and break it down into tokens, just like the String Tokenizer does. However, I can't seem to figure out how to do it since I can't break it down by white space (although, I guess I could tokenize and then count to see if it is the 4th item to determine if I should do something different.

What is the most efficient way to handle this?

Rob Spoor

Joined: Oct 27, 2005
Posts: 20276

java.util.Scanner. You can use the next() and nextInt() methods. The . can be found using next(String pattern) using the right pattern.

You could also use java.util.regex.Pattern in combination with java.util.regex.Matcher. The regular expression is quite simply, and using capturing groups you will get a simple loop where you can retrieve all 5 values in the body.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: Parsing out text in a predictable pattern
It's not a secret anymore!