Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

StringTokenizer countTokens problem

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This code seems straight forward. I'm trying to make sure my parens are balanced. The output from the following is:
Num left parens: 1
Num right parens: 2
Why this result when both values should be 1?
thanx in advance, vasha
import java.util.*;
public class Test{
private static void main (String[] args){

try{
String s = "(Q AND Q) OR Q";
int iLeft=0;
int iRight=0;
StringTokenizer stLeft = new StringTokenizer(s,"(");
iLeft = stLeft.countTokens();
StringTokenizer stRight = new StringTokenizer(s,")");
iRight = stRight.countTokens();
dbg("Num left parens: "+iLeft);
dbg("Num right parens: "+iRight);
}
catch (Exception ex)
{
dbg("Exception: "+ex);
}
}
private static void dbg(String asMessage)
{
System.out.println(asMessage);
}
}
 
Sheriff
Posts: 16948
286
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
stLeft has the following token: "Q AND Q) OR Q"
stRight has the following tokens: "(Q AND Q" and " OR Q"
You can see this for yourself with the following code:
<pre>
StringTokenizer stLeft = new StringTokenizer(s, "(");
while (stLeft.hasMoreTokens())
{
System.out.println(stLeft.nextToken());
}

StringTokenizer stRight = new StringTokenizer(s, ")");
while (stRight.hasMoreTokens())
{
System.out.println(stRight.nextToken());
}
</pre>
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you create a StringTokenizer as new StringTokenizer(s,"("), the "(" characters are delimiters, not tokens. The tokens are the parts of the string between the delimiters. Since there is no countDelimiters() method, the StringTokenizer class is of limited use to you here. Better to forget about StringTokenizer and just write a loop that counts parenthesis directly:
<code><pre>
int leftCount = 0;
int rightCount = 0;
for (int i = 0; i < s.size(); i++) {
char c = s.charAt(i);
if (c == '(') {
leftCount++;
}
else if (c == ')') {
rightCount++;
}
}
System.out.println("Left parens: " + leftCount);
System.out.println("Right parens: " + rightCount);
</pre></code>
 
Vasha Qafiri
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks kindly, I suppose I misunderstood the usage of the StringTokenizer and its methods!
vasha
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.
You can go this way: there is a constructor for StringTokenizer that accept 3 parameters and third one says include or exclude delimiter from calculation. So you create StringTokenizer first with 3 parameter constructor, call to countTokens() to receive general number of tokens (inlcudes delimiters), than you create StringTokenizer again but with 2 parameters constructor (this way delimiters are not calculated in countTokens() function call.
After that you just make countToken1 - countToken2 and you have number of delimiter in your string.
Hope it helps.
Alex.
 
Come have lunch with me Arthur. Adventure will follow. This tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic