StringTokenizer bug?

Juergen Sonnauer kaffe@rufus.w3.org
Sun, 26 Jul 1998 13:48:12 +0000 ()


The following example shows a bug which i cannot resolve at the
moment. It might be in the class StringTokenizer or in the native
AWT layer. I am not sure if this reproduces in my environment only
(Linux i386 JIT, Kernel 2.0.34). Can anyone help.

--
import java.awt.*;
import java.util.*;

public class TestIt extends Canvas {
   Hashtable hash = new Hashtable(5);
   public TestIt() {
      String line = "one:Hello:two:Party:three:Fun:four:Beer";
      StringTokenizer strtok = new StringTokenizer(line, ":");
      String key, val;
      while (strtok.hasMoreTokens()) {
         key = strtok.nextToken();
         val = strtok.nextToken();
         hash.put(key, val);
         System.out.println("Key: "+key+", Value: "+val);
      }
   }
   public void paint(Graphics g) {
      // This one does not work: 
      // cast from object to string and pass to native function
      // ==> the canvas shows "one:H", stdout shows "Party"
      String text = (String) hash.get("two");
      System.out.println("drawString: "+text);
      g.drawString(text, 10, 20);

      // Workaround: 
      String text2 = new String((String) hash.get("two"));
      System.out.println("drawString: "+text2);
      g.drawString(text2, 10, 40);
   }
   public static void main ( String[] args ) {
      Frame f = new Frame("TestIt");
      TestIt t = new TestIt();
      f.add(t);
      f.setBounds(10,10,150,80);
      f.setVisible(true);
   }
}

Thanks for your time.
-- 
Juergen Sonnauer
eMail: sonic@az-online.net