wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Parse String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Parse String" Watch "Parse String" New topic
Author

Parse String

David O'Connel
Greenhorn

Joined: May 13, 2009
Posts: 1
I am taking a string input from the command line using a buffered reader and storing in a string.

This specific string is in the form USE <item> ON <item> where an item can be one or more words.

I am trying to store these two specified items in two separate strings, I have had trouble with both the string tokenizer and the .split() method.

When using the string tokenizer my loop gets far to confusing and ends up not working at all.

Where as with .split() I am having trouble coming up with the correct reg-ex to split it up as I want.

currently for the regular expression i am trying to use "\b[Oo][Nn]\b" to split around the ON or any upper/lower case variation.

However when I print out the array that is returned it just returns the string in its original form.

Any help would be greatly appreciated, thanks in advance.

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11175
    
  16

The first thing i would do would be to normalize your string (if you can). convert the whole thing to uppercase, then you don't have to worry about [oO] type nonsense.

there are many ways to approach this. One way might be to find the position of the substring " on ". you would then know that the relevant portion for the first item would be from the 4th position to the start of that substring. and since you know how long the " on " substring is, you can get whatever comes after it fairly easily.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
David:

Welcome to Java Ranch!

Java since Java regexes are Strings, you need to use double backslashes, like this:

Otherwise, Java will interpret the backslashes as escape sequences.

Another approach to your problem might be to split on one or more whitespace characters.

John.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Parse String
 
Similar Threads
Converting Date into Day, Month, Year
FBN: criteriaFind(String criteria) method; is this ok ?
Problem in StringTokenizer
Punction Correct (Sentances)
String Tokenizer or Regex