Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about StringTokenize===>

 
David Huang
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following code:

do analyze(String s){

StringTokenizer st = new StringTokenizer(s, "/" ,false);

this.address = st.nextToken();
this.dateq=Integer.parseInt(st.nextToken());
}

what if the passing String 's' dosen't contain '/'?

then I will get an empty set of variables..

is it possible to check whether the String s contains '/' then proceed my code ...?

I am a java beginner, can anyone just tell the exact code of doing that?


Many thanks



 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the String doesn't contain a '/' the whole String will be returned from the first call to nextToken().

You can test for a '/' in the string with the following


You can also get the number of tokens by calling countTokens() on the StringTokenizer
 
Damanjit Kaur
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
StringTokenizer st = new StringTokenizer(s, "/" ,false);

this.address = st.nextToken();
this.dateq=Integer.parseInt(st.nextToken());
}

what if the passing String 's' dosen't contain '/'?


The StringTokenizer constructor that you are using won't return any token including '/' character, because of the third argument in constructor which is false. The second argument is used to denote delimiters/separators between the tokens.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic