Running on Java 22-ea+27-2262 (Preview)
Home of The JavaSpecialists' Newsletter

119Book Review: "Wicked Cool" Java

Author: Dr. Heinz M. KabutzDate: 2006-01-13Java Version: 1.3Category: Book Review
 

Abstract: The book "Wicked Cool Java" contains a myriad of interesting libraries, both from the JDK and various open source projects. In this review, we look at two of these, the java.util.Scanner and javax.sql.WebRowSet classes.

 

Welcome to the 119th edition of The Java(tm) Specialists' Newsletter. In March I will be speaking at TheServerSide Java Symposium in Las Vegas. My topics are: Java Specialists in Action and Productive Coder.

javaspecialists.teachable.com: Please visit our new self-study course catalog to see how you can upskill your Java knowledge.

Book Review: Wicked Cool Java [ISBN 1593270615]

I must admit that the title of the book put me off a bit. In South Africa the adjective "wicked" has a dark meaning. Not so in the USA, where it nowadays means: strikingly good, effective, or skillful. Add "cool" to that, and it becomes: "strikingly super cool Java". And that, dear newsletter reader, pretty much sums up the contents of this book!

This book is more than just a rehash of the JavaDoc changes. It points you to all sorts of cool utilities and open source projects that let you do things from playing music to writing neural networks. "Wicked Cool". At the same time, it does not bore you with mountains of detail. Perfect for The Java(tm) Specialists' Newsletter readers.

As I usually do with book reviews, I will highlight a few nice gems from the book, and you can then buy "Wicked Cool Java" on Amazon [ISBN 1593270615] or your local bookshop.

java.util.Scanner

The first thing in the book to catch my eye was the new java.util.Scanner class which Sun added in Java 5. It helps us parse text on an input stream, which previously we had to do with BufferedReader, StringTokenizer and various text parsers.

Given the following text file, it would read the columns and parse them at the same time:

    file logfile.txt:
    -----------------

    entry 2006 01 11 1043 meeting Smith, John
    exit 2006 01 11 1204 Smith, John
    entry 2006 01 11 1300 work Eubanks, Brian
    exit 2006 01 11 2120 Eubanks, Brian
    alarm 2006 01 11 2301 fire This was a drill

Here is how you could use the java.util.Scanner class:

import java.util.Scanner;
import java.io.*;

public class ScannerTest {
  public static void main(String[] args) throws IOException {
    Scanner scanner = new Scanner(new FileReader("logfile.txt"));
    while(scanner.hasNext()) {
      String type = scanner.next();
      int year = scanner.nextInt();
      int month = scanner.nextInt();
      int day  = scanner.nextInt();
      int time = scanner.nextInt();
      System.out.printf("%d/%d/%d@%d", year, month, day, time);
      if (type.equals("entry")) {
        String purpose = scanner.next();
        String restOfLine = scanner.nextLine().trim();
        System.out.printf(" entry %s: %s%n", purpose, restOfLine);
      } else if (type.equals("exit")) {
        String exitName = scanner.nextLine().trim();
        System.out.printf(" exit %s%n", exitName);
      } else if (type.equals("alarm")) {
        String alarmType = scanner.next();
        String comment = scanner.nextLine().trim();
        System.out.printf(" alarm %s: %s%n", alarmType, comment);
      } else {
        throw new IllegalArgumentException();
      }
    }
    scanner.close();
  }
}

The output of the program is:

    2006/1/11@1043 entry meeting: Smith, John
    2006/1/11@1204 exit Smith, John
    2006/1/11@1300 entry work: Eubanks, Brian
    2006/1/11@2120 exit Eubanks, Brian
    2006/1/11@2301 alarm fire: This was a drill

Scanner can parse primitive types, BigDecimal and BigInteger. Thus, you could write: scanner.nextBigDecimal() or scanner.nextBoolean().

javax.sql.WebRowSet

Another interesting addition to Java 5 is javax.sql.WebRowSet, implemented by com.sun.rowset.WebRowSetImpl. You should not use classes from com.sun.* packages directly in your code. Rather store the implementation class name in a configuration file and create the WebRowSet implementation using a factory class.

WebRowSet can generate and read XML based on a well-defined schema, found on https://java.sun.com/xml/ns/jdbc/webrowset.xsd. You can use them by either giving them a ResultSet instance, or by providing the properties of what should be executed. In Wicked Cool Java [ISBN 1593270615] , Brian Eubanks executes a query and passes the ResultSet to the WebRowSet instance using the populate(ResultSet) method. In my example, I will show an alternative approach:

import com.sun.rowset.WebRowSetImpl;
import javax.sql.rowset.WebRowSet;

public class WebRowSetTest {
  public static void main(String[] args) throws Exception {
    if (args.length != 5) {
      System.err.println("Usage: java WebRowSetTest " +
          "driver url user password table");
      System.exit(1);
    }

    int column = 0;
    String driver = args[column++];
    String url = args[column++];
    String user = args[column++];
    String password = args[column++];
    String table = args[column++];

    Class.forName(driver);

    WebRowSet data = new WebRowSetImpl(); // rather use a factory
    data.setCommand("SELECT * FROM " + table);
    data.setUsername(user);
    data.setPassword(password);
    data.setUrl(url);
    data.execute(); // executes command and populates webset

    data.writeXml(System.out);
    data.close();
  }
}

Other Cool Stuff

In preparation for our move to Crete, Greece, I have started playing tavli, similar to backgammon. You can find me frequenting Tavli-Mania, where Greeks from all around the world come to pass their time. I prefer Tavli to Chess, since even the worst player can beat a pro. Strategy is important, but the dice can turn a game. Yesterday I had a ten game losing streak, but this morning I beat the #1 player. Imagine beating the #1 chess player!

An interesting bit of information that I discovered on Wikipedia: Backgammon computer games work better with neural networks than with brute force, like chess. My brain has never been brute force, which might explain my ineptitude at chess :)

In the book, Brian Eubanks mentions an open source neural network engine called Joone (Java Object Oriented Neural Engine), which even includes a graphical editor. I wish I knew more about neural networks to really appreciate this tool.

Oh, the book also mentions tools for genetic algorithms, intelligent agents and computational linguistics. Then we have scalable vector graphics, XML based Swing layouts and to top it off, code on how to record sound from within Java and a library for synthesizing speech.

Fun book to read, with just enough detail to get you searching in different directions. Not for Java beginners. But rather, Wicked Cool Java [ISBN 1593270615] is for the wicked Java programmer ;-)

Kind regards

Heinz

 

Comments

We are always happy to receive comments from our readers. Feel free to send me a comment via email or discuss the newsletter in our JavaSpecialists Slack Channel (Get an invite here)

When you load these comments, you'll be connected to Disqus. Privacy Statement.

Related Articles

Browse the Newsletter Archive

About the Author

Heinz Kabutz Java Conference Speaker

Java Champion, author of the Javaspecialists Newsletter, conference speaking regular... About Heinz

Superpack '23

Superpack '23 Our entire Java Specialists Training in one huge bundle more...

Free Java Book

Dynamic Proxies in Java Book
Java Training

We deliver relevant courses, by top Java developers to produce more resourceful and efficient programmers within their organisations.

Java Consulting

We can help make your Java application run faster and trouble-shoot concurrency and performance bugs...