Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

Book Review: "Wicked Cool" Java

JavaFAQ Home » Story by Dr. Kabutz Go to all tips in Story by Dr. Kabutz


Bookmark and Share

The Java Specialists' Newsletter [Issue 119]

Author: Dr. Heinz M. Kabutz

JDK version: JDK 1.3+

Category: Book Review

If you are reading this, and have not subscribed, please consider doing it now by going to our subscribe page [http://www.javaspecialists.co.za/archive/subscribe.jsp]. You can subscribe either via email or RSS.


Welcome to the 119th edition of The Java(tm) Specialists' Newsletter. In March I will be speaking at TheServerSide Java Symposium in Las Vegas [http://www.javaspecialists.co.za/talks/tssjs06] . My topics are: Java Specialists in Action and Productive Coder. I am presenting a sneak preview of "Productive Coder" [http://www.javaspecialists.co.za/talks/tssjs06/index.jsp#prodcod] in Cape Town next Wednesday, the 18th of January. Please come if you happen to be in Cape Town.

We are running a Java Standard Edition course in Cape Town [http://www.javaspecialists.co.za/capetown/index.jsp] from the 13th to 17th Feb 06, which I will be presenting. The best time of year to come to Cape Town Smile Whilst the northern hemisphere is a solid block of ice, we have stunningly warm cloudless days, balmy nights, and the wind has calmed down. Right now I am sitting with my laptop in the shade of my palm tree watching my kids enjoying our pool. And February is even better ...

Book Review: Wicked Cool Java

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 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 http://java.sun.com/xml/ns/jdbc/webrowset.xsd [http://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, 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 Java courses in Crete, Greece [http://www.javaspecialists.co.za/crete] , I have started playing tavli, similar to backgammon. You can find me frequenting Tavli-Mania [http://www.tavli-mania.com] , 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 [http://en.wikipedia.org/wiki/Backgammon] : 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 Smile

In the book, Brian Eubanks mentions an open source neural network engine called Joone (Java Object Oriented Neural Engine) [http://www.jooneworld.com] , 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 is for the wicked Java programmer Wink

Kind regards

Heinz


Copyright 2000-2005 Maximum Solutions, South Africa

Reprint Rights. Copyright subsists in all the material included in this email, but you may freely share the entire email with anyone you feel may be interested, and you may reprint excerpts both online and offline provided that you acknowledge the source as follows: This material from The Java(tm) Specialists' Newsletter by Maximum Solutions (South Africa). Please contact Maximum Solutions [http://www.javaspecialists.co.za] for more information.

Java and Sun are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. Maximum Solutions is independent of Sun Microsystems, Inc.

 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy