This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Parsing out text in a predictable pattern" Watch "Parsing out text in a predictable pattern" New topic
Author

Parsing out text in a predictable pattern

Terry Chambers
Greenhorn

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?

Thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Parsing out text in a predictable pattern
 
Similar Threads
Do while error, I'm going crazy
Beer Song
AOOB exception in my CODE-PLEASE HELP!
Numeric value convert to word format
need help figuring this out.