• 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

Parse String

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic