This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes stingTokenizer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "stingTokenizer" Watch "stingTokenizer" New topic
Author

stingTokenizer

Linda M Wilson
Greenhorn

Joined: May 27, 2004
Posts: 3
The string in the my stringTokenizer blows ups with error "java.util.NoSuchElementException"
I see that in my stringTokenizer, it contains only part of the string, it does no show the comma (,) that is in the string.
Part of the stringTokenizer contains a a fullname (one field) "lastName, firstName". So it looks like it blows up when it reaches the comma. I cannot strip the comma off of a fullname field otherwise when querieing against the database it will not find it without the comma. Can I use some kind of "escape sequence" for the comma in the stringTokenizer to prevent it from blowing up? If so, how?
Thanks.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

I assume that you are tokenizing your "lastname, firstname" string with the delimiter "," yes? In which case you probably won't see the comma because it is a token, not an element. For example:

would create a tokenizer containing two elements ("lastname", " firstname") and one token (",").

A quick glance at the JavaDoc for StringTokenizer and you discover that the possible causes of a NoSuchElementException being thrown is is there are no more tokens in a tokenizers string. This code will throw that exception:

If you are accessing the contents of a StringTokenizer, make sure you access them in a safe way, e.g.:


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Linda M Wilson
Greenhorn

Joined: May 27, 2004
Posts: 3
No the comma in the fullname "lastName, firstName" is not the delimeter, it is part of the whole string, the delimeter is a pipe (|). Can I get it to not read or escape this the comma in the fullname?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

OK I follow now. The string you are tokenizing is "lastname, firstname" yes? And you are tokinizing on the delimiter "|" which doesn't feature in the string so you have a one element tokenizer with no tokens. Whatever, there are still only two ways to throw a NoSuchElementException, either by calling tokenizer.nextToken() or tokenizer.nextElement() when there are no more elements or tokens in the tokenize.

Perhaps you could post some code so we can see what's going on.
Linda M Wilson
Greenhorn

Joined: May 27, 2004
Posts: 3
stringTokenizer =
new StringTokenizer(strContactedInfo, Constants.DELIMITER_PIPE);

fullname gets passed into strContactedInfo

I think the comma can be replaced with a different character for example a tilde?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Well you could replace the comma, and its not that difficult. But once again I'll have to say that is with 100% centainty not what is causing the NoSuchElementException.

Presuming you are passing in "lastname, firstname" into your tokenizer and you are delimiting it with a "|" character (I have no idea whether your Constants class actually defines the DELIMITER_PIPE as a literal string "|"), then you will get a tokenizer with one element and no tokens in it. The element will be: "lastname, firstname". And the only way to get a NoSuchElementException is by calling nextElement() or nextToken() on this tokenizer in an unsafe way.

[ May 27, 2004: Message edited by: Paul Sturrock ]
[ May 27, 2004: Message edited by: Paul Sturrock ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: stingTokenizer
 
Similar Threads
Program i need help bad
please review criteriaFind -> good riddance from hateful "ANY"
Converting String to an array.
String question
Is StringTokenizer a "Java Gotcha" for use in CSV processing?