Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

More Java Pitfalls Share Reactor - Daconta M,C.

Daconta M,C. More Java Pitfalls Share Reactor - Wiley publishing, 2003. - 476 p.
ISBN: 0-471-23751-5
Download (direct link): morejavapitfallssharereactor2003.pdf
Previous << 1 .. 50 51 52 53 54 55 < 56 > 57 58 59 60 61 62 .. 166 >> Next

43 {
44 int delimldx = input.indexOf(delimiters,start) ;
45 if (delimldx < 0)
46 {
47 String tok = input substring(start);
48 l.add(tok);
49 start = end;
50 }
51 else
52 {
53 String tok = input substring(start,
delimldx);
54 l.add(tok);
55 start = delimldx + delimiters.length() ;
56 }
57 }
58 }
59
60 return l;
61 }
62
63 public static void main(String args[])
64 {
65 try
66 {
Listing 18.2 (continued)
144 Item 18
67 String delim = "###";
68 String input = "123###4#5###678###hello###wo#rld###9" ;
69 // expecting 1 2 3 4 5 6
tokens
70 System.out.println("input: " + input);
71 System.out.println("delim: " + delim);
72 System.out.println("If '###' treated as a group 2
delimiter expecting 6 tokens...");
73 String [] toks = tokenize(input, delim, true);
74 for (int i=0; i < toks.length; i++)
75 System.out.println("tok[" + i + "]: " + toks[i]);
76 System.out.println("# of tokens: " + toks.length);
77 } catch (Throwable t)
78 {
79 t.printStackTrace();
80 }
81 }
82 }
83
Listing 18.2 (continued)
Following is run of GoodStringTokenizer that demonstrates the new static method, tokenize(), that treats the token String “###" as a single delimiter:
>java org.javapitfalls.util.mcd.i1.GoodStringTokenizer input: 123###4#5###678###hello###wo#rld###9 delim: ###
If '###' treated as a group delimiter expecting 6 tokens...
tok[0]: 123
tok[1]: 4#5
tok[2]: 678
tok[3]: hello
tok[4]: wo#rld
tok[5]: 9
# of tokens: 6
Beyond solving the “delimiter as a group" problem, GoodStringTokenizer adds a utility method to convert the set of tokens into a java Collection. This is important, as StringTokenizer is a pre-Collection class that has no built-in support for collections. By returning a collection, we can take advantage of the utility methods, specifically, those for sorting and searching, in the Collections class. The class below, TokenCollectionTester.java, demonstrates the benefits of a Collection of tokens.
Effective String Tokenizing 145
01 package 8 1 m e t i s l l a f t i p a v a j g. r o
02
03 import java.util.*;
04
05 public class TokenCollectionTester
06 {
07 public static void main(String args[])
08 {
09 try
10 {
11 String input = "zuchinni, apple, beans, hotdog, ^
hamburger," +
12 "wine, coke, drink, rice, fries, chicken";
13 String delim = ", ";
14 List l = GoodStringTokenizer.toksToCollection(input,
15 delim, false);
16 String top = (String) Collections.max(l);
17 System.out.println("Top token is: " + top);
18 Collections.sort(l);
19 System.out.println("Sorted list: ");
20 Iterator i = l.iterator();
21 while (i.hasNext())
22 System.out.println(i.next());
23
24 } catch (Throwable t)
25 {
26 t.printStackTrace();
27 }
28 }
29 }
Listing 18.3 TokenCollectionTester.java
Running TokenCollectionTester produces the following output:
>java org.javapitfalls.util.mcd.il.TokenCollectionTester
Top token is: zuchinni
Sorted list:
apple
beans
chicken
coke
drink
fries
hamburger
146 Item 19
wine
hotdog
rice
zuchinni
In this item, we have carefully examined the workings of the StringTokenizer class, highlighted some shortcomings, and created some utility methods to improve the class.
Item 19: JLayered Pane Pitfalls9
While working on the jXUL project (an open-source effort to integrate XUL, or Extensible User-Interface Language, with Java) for the book Essential XUL Programming, I ported a Pacman arcade game clone called Pagman to a Java-based XulRunner platform. XulRunner is a Java class that executes XUL applications; it's similar to the JDK's AppletRunner. Figure 19.1 provides a screen shot of Pagman port's current version, which successfully allows the ghost sprites to move on a JLayeredPane's top layer. The sprites move over the background images, which exist in a layer beneath. (Many thanks to my coauthor Kevin Smith, who worked through these pitfalls with me to bring Pagman to fruition.)
Instead of examining this pitfall in the XulRunner code, which is rather large, we will examine a simpler example that demonstrates the problem. Those interested in the Pagman code can download it from the jXUL Web site (http: / /www.sourceforge .net/jxul).
Our simple BadLayeredPane example in Listing 19.1 attempts to create a frame that has a colored panel in a background layer and a button in a foreground layer with a JLayeredPane:
Figure 19.1 Pagman using a JlayeredPane.
Graphics © Dan Addix, Brian King, and David Boswell.
9 This pitfall was first published by JavaWorld (www.javaworld.com) in the article, “Practice makes perfect" November 2001 (http://www.javaworld.com/javaworld/jw-11-2001/jw-1116-traps .html?) and is reprinted here with permission. The pitfall has been updated from reader feedback.
JLayered Pane Pitfalls 147
01 package org.javapitfalls.item19;
02
03 import java.awt.*;
04 import javax.swing.*;
05 import java.awt.event.*;
Previous << 1 .. 50 51 52 53 54 55 < 56 > 57 58 59 60 61 62 .. 166 >> Next