Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Alternative to string tokenzier

 
Saral Saxena
Ranch Hand
Posts: 203
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks ,

I am currently using StringTokennizer class to split a String into different tokenas by defined delimiter


My query is that did jdk 1.5 , provides us with any other alternatives to achieve this same thing like regex and matching pattern or split() of string class..!!please advise..!!
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it was discussed enough here:
http://www.coderanch.com/t/326006/java/java/String-split-Vs-StringTokenizer
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use split ...

Then iterate through the tokens[] array.

Pat.
 
Saral Saxena
Ranch Hand
Posts: 203
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William P O'Sullivan wrote:Use split ...

Then iterate through the tokens[] array.

Pat.


Hi William ,

can you show through split(), it would be a great help if you could convert mine program , in your approach , showing the use of split()..!!thanks in advance..!
 
Saral Saxena
Ranch Hand
Posts: 203
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William P O'Sullivan wrote:Use split ...

Then iterate through the tokens[] array.

Pat.


My second query is that which one we should choose tokenzier of split()..??please advise..!! thanks in advance..!!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saral Saxena wrote:
William P O'Sullivan wrote:Use split ...

Then iterate through the tokens[] array.

Pat.


Hi William ,

can you show through split(), it would be a great help if you could convert mine program , in your approach , showing the use of split()..!!thanks in advance..!


Please try doing your own work, rather than asking for someone to do it for you.
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I already gave you the split() code, now all you have to do is change the while()
to something else ... ;)

as for which one to use, depends on the need.

StringTokenizer has the ability to return the token as one of the elements, in your case ","
would appear in the collection of tokens. split() does not include the delimiter.

Pat.


 
Saral Saxena
Ranch Hand
Posts: 203
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks ,

I am currently using StringTokennizer class to split a String into different token as by defined delimiter


My query is that can same thing can also be achieved through scanner class also ...!! Is it the right approach to use the scanner class since I was reading The Scanner class allows you to tokenize data from within a loop, which allows you to stop whenever you want to...
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saral Saxena wrote:
My query is that can same thing can also be achieved through scanner class also ...!!


So study the details of the Scanner class, and see if you can make it work that way.

And really, a single punctuation mark is sufficient to end a sentence. More that is just distracting clutter that makes your post unpleasant to read.
 
Saral Saxena
Ranch Hand
Posts: 203
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Saral Saxena wrote:
My query is that can same thing can also be achieved through scanner class also ...!!


So study the details of the Scanner class, and see if you can make it work that way.

And really, a single punctuation mark is sufficient to end a sentence. More that is just distracting clutter that makes your post unpleasant to read.


Hi Jeff,

I have tried this but it not producing the result please guide me ,,how to correct this to produce the result..
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saral Saxena wrote:

I have tried this but it not producing the result please guide me ,,how to correct this to produce the result..


ItDoesntWorkIsUseless.(⇐click) You have to TellTheDetails(⇐click) of what exactly you expected to happen and what exactly happened instead.

And by now, with all the threads you've posted and all the times you've been reminded, you should know that you need to show some effort and that you need to tell the details without someone having to remind you every single time.

Please keep this in mind for future posts. Thanks.
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really dislike StrinkgTokenizer(), I think it should be @deprecated and removed. For most complex splits, I much prefer the Google Guava packages.

com.google.common.base.Splitter

for this specific case.

 
Saral Saxena
Ranch Hand
Posts: 203
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pat Farrell wrote:I really dislike StrinkgTokenizer(), I think it should be @deprecated and removed. For most complex splits, I much prefer the Google Guava packages.

com.google.common.base.Splitter

for this specific case.



output i need such as....

---- Split by comma ',' ------
This is String
split by StringTokenizer
created by saral
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic