Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

stingTokenizer

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.:
 
Linda M Wilson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic