• Post Reply Bookmark Topic Watch Topic
  • New Topic

Tokenizing a Scanner input string with random number of words and random number of spaces in between  RSS feed

 
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

As a part of one requirement I am currently working on, I need to tokenize Scanner input into String tokens. In the input each word is separated by blanks, but some users may enter more than one intermediate blanks by mistake. This is what I need to handle in my code. I am using <input.trim()>.split(" "); to tokenize the input.

I can have inputs comprising of

1. a single word.
This case I can handle by using <String>.trim() method.

2. more than one word input.
I can get rid of starting and trailing spaces before tokenizing by using the trim method. But how do I get the exact words if the user has entered more than one space between the words.
This needs to be sort of a recursive replace so no matter how many intermediate spaces are there, they would all be changed into just one intermediate space. So I can't use <string>.replaceAll(" ", " ") method.
I'm quite certain that this can be done using regular expressions and I should study Regular expressions and they are in my study plan. But currently if you could help me, it'd be great.

Each word is alphanumeric and has no other special characters.

Please help.

Thanks,
Chan.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, this is resolved.

I have tried the following.



Thanks,
Chan.
 
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Wow! I didn't even know this was possible. Thanks for sharing.
Have a cow. Moo!
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you :-)

I love these pretty cows. Please don't take 'em back if I write stupid posts some day. Do you all take them back also?

Chan.






 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chan Ag wrote: Please don't take 'em back if I write stupid posts some day. Do you all take them back also?

Only if some mod is hungry
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!