Alvin Alexander | Java, Scala, Unix, Perl, Mac OS X

Can you show me a Java StringTokenizer example?

Sure. The Java StringTokenizer class can be used to split a Java String into tokens. Here's a simple StringTokenizer example:

// start with a String of space-separated words
String tags = "pizza pepperoni food cheese";

// convert each tag to a token
StringTokenizer st = new StringTokenizer(tags," ");

while ( st.hasMoreTokens() )
  String token = (String)st.nextToken();

This segment of code will have this output:

Question: How do I create a comment in Java?

Answer: There are two ways to create Java comments, and both are shown in the example code below:

// this is a one-line comment

 * this is also
 * a comment, but it can span multiple
 * lines.

The // syntax makes everything after it on the current line a Java comment, and the /** ... */ syntax lets you create multi-line Java comments.

Java 5 FAQ: Can you share some examples of the Java 5 for loop syntax?

Sure. As a bit of background, in the days before Java 5 you could create a for loop to iterate over a collection of strings like this:

// assumes there is a method named "getList()"
List list = getList();

for (Iterator it = list.iterator(); it.hasNext();) {
  String value=(String);

Java 5 for loop syntax

That’s not too bad, but with the release of Java 5 your for loops can now be a little tighter, like this:

Java for loop FAQ: Can you show me an example of a Java for loop?

Answer: Sure. Here's an example of a Java for loop using the original Java syntax:

Java while loop FAQ: Can you show me an example of a Java while loop?

Sure, here's a Java while loop example:

package com.devdaily.javasamples;

public class WhileTest
  public static void main(String[] args)
    int i = 0;
    while (i < 5)
      System.out.println("i = " + i);

This example prints out the value of the variable i until i gets to 5, at which point the loop stops.

Question: Can you show me an example of the Java if/then syntax?

Answer: Sure, here you go:

Java switch case statement FAQ: Can you provide an example of a Java switch/case statement syntax?

Here's a sample Java method that takes an int argument and attempts to turn that int into a month string, using a Java switch case statement to make that decision:

As I started to mention in another blog post, your Java code will be more flexible when you learn to return more-general object references. In most cases other developers only need to see your interface, not your implementation. Put another way, does it matter to anyone else if you used a LinkedList or an ArrayList? If it doesn't matter, then return a List, or perhaps even a Collection.

I find that I learn a lot — especially initially — when I can see source code examples. To that end, here’s some sample code showing how to use a Java LinkedList. This uses Java syntax prior to Java 5:

A friend sent me a link to this story about Google planning a service to store user's data. I read the article, and all I can say is "no thanks". I might use it as a backup service, but I sure don't want to make it my primary copy.

Count me in as one of the football fans who are upset about tonight's game between the Green Bay Packers and the Dallas Cowboys being shown on the NFL Network. Okay, compared to a lot of other things this is a pretty trivial concern, but moving games to a premium cable channel sets a bad precedent. The NFL can do whatever they want, but this being America, consumers can also say "Screw you NFL", and this is your time to vote.

Note: Amazingly, I wrote this review way back in 2007. I just tried to use Poseidon UML again in 2010, and I wouldn't change a word of it. (Okay, I'll change one thing: I now use UMLet instead of MagicDraw UML.)

A nice feature of Eclipse is that you can easily generate hashCode and equals methods for your Java class using the editor. You'll really appreciate this capability when you need to create these methods whenever you're doing anything related to sorting, comparisons, comparators, etc.

I just learned that Eclipse can automatically create (or suggest) variable names for you. The suggestions are based on the class of the object you're about to create, and any characters you've typed for the variable name so far.

For example, if I'm in a method and I type Connection, followed by a space, then press the [Control][Space] keys, Eclipse adds a variable named connection, so my line changes to Connection connection.

Using Eclipse, if your cursor is positioned in between the parentheses of a method call, the [Control][Spacebar] keystroke will show you the signature of the method, i.e., the parameters that the method accepts. I just read where the documentation shows that you should use the [Control][Shift][Spacebar] keystroke to see method parameters, but I've found that [Control][Spacebar] works fine for me.

I've just recently started using Eclipse a lot, and I just typed System.err.println() one more time than I cared to. I just looked it up, and Eclipse also has templates, and one of the pre-built templates lets me type sys, then hit the [Ctrl][Spacebar] keystroke. This brings up a list of options I can choose from, one of those options being to insert the System.err.println() text in place of my sys text.

Java File I/O FAQ: Using Java, how can you test to see if a file or directory exists?

Java test to see if a file or directory exists

Answer: Here's a sample Java method that shows this test. All you have to do is use the exists() method of the File class ( to perform this test.

Develop with pleasure. That's the IntelliJ IDEA slogan, and at least through Version 5 I think they nailed it on the head.

So this product review is simple: If you're a Java developer go get an eval copy of IntelliJ IDEA right now and give it an honest eval. You won't be sorry.

Working on tag support for this blog, I've cut a little sample code out of a Java servlet, and I'm showing it below. I was looking at the HttpServletRequest, and was curious about the difference between request.getPathInfo(), request.getPathTranslated(), and request.getRequestURI(), so I created this demo code. Here's the example code from the servlet:

AppleScript Finder FAQ: How do I get the full Mac OS X Finder path in an AppleScript script?