Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String Tokenizer

 
lphung
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,
During the use of the StringTokenizer class, I came across a string in the format "1","ABC","Last, First","CAT","MBA"
If i use the comma as a my delimiter, how do I handle the "," in inside the double quoted string? Especially when I do a countTokens(), it returns 6 tokens instead of 5.
Any help would be greatly appreciated.
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Iphung",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please log in with a new name which meets the requirements.
Thanks.
 
John Bateman
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I Since everything is a string in your example..
use "," as the 'token'.. BUT, you will have to use indexOf to get a position of each token and then subString() the token out of the String. This is because the StringTokenizer will consider the "," as a list of tokens to check for.
Ex:
"Hi","there","me,you"
Would give you 3 tokens.
token 1> "Hi
token 2> there
token 3> me, you"
Now all you have to do is chop the 1st char off the 1st token and the last char off the last token.
Just my $0.02cdn
 
Art Metzer
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's all I could think of, and I of course defer to any better solutions.
1. Go through your original String, replacing all commas between quotes with a placeholder character that you can guarantee will never ever appear in any input String.
2. Tokenize this altered String.
3. When you receive each token, replace each placeholder you find with a comma.
4. Process the token normally.
Here's a bit of code to demonstrate:

Will something like this do the trick?
Art
[This message has been edited by Art Metzer (edited April 06, 2001).]
 
Angela Jessi
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you can use indexOf. Then get the length of string.
Here is an example:
int iLen;
iEnd = sToken.indexOf(':', iEnd);
sTemp = sToken.substring(iBegin, iEnd);
iBegin = iEnd + 1;
iEnd++;
if( iFlag == 1 ) // Parsing to get the values of arguments
{
iLen = Integer.parseInt(sTemp);
sTemp = sToken.substring(iBegin, iBegin + Len);
iEnd = iBegin + iLen + 1;
iBegin = iEnd;
}
return sTemp;

Thanks,
Angela
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic