Regexp fun

Reading the post about useful regular expressions,  remembered what my favourite solution is to one of  the questions of the test we give to junior Java developers.

The task is to write a method that takes a string as a parameter and returns the acronym of the string in uppercase made up of the first letters of the words in the string. The acronym must ignore the words “the”, “of” and “and”.

The usual solutions are either to sequentially step through the string (Yuck!) or split it up or use a StringTokenizer class. The people usually overlook the fact, that the input strings can be padded with whitespace, or contain multiple spaces, and they usually ignore, that the keywords that are to be omitted might be found on the begining of a valid word. Thus my test ” United   States of Andorra” string breaks most of the methods.  The ones who have time to write the answer down, usually forget to return the value from the method, or to change it to uppercase and sometimes even ignore that it should be a method to start with! This is my favourite question, as it can really show how the applicant can handle stressful situations.

I was tired after several interviews one day and tried to come up with the  shortest possible solution. Naturally it contains regular expressions.

My solution looked something like this (OK I just reproduced it for the sake of the article, using nano and javac, so it might have overlooked flaws in it):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Acronym { 
 
private static String toAcronym(String str) {
        return str.toUpperCase().
                        replaceAll("(THE|OF|AND)(\W+|$)","").
                        replaceAll("(\w)\w*\W*","$1");
}
 
public static void main (String args[]) {
                if(args.length>0) {
                        System.out.println(toAcronym(args[0]));
        }
}
 
}

The Parable of the Object Oriented Programmer and Breakfast

This is the English version of a tale I’ve recently found in my archive mailbox from 2001. Looking at ongoing projects, I think it still has a point.

Once upon a time, in a kingdom not far from here, a king summoned two of his advisors for a test. He showed them both a shiny metal box with two slots in the top, a control knob, and a lever. “What do you think this is?
Continue reading “The Parable of the Object Oriented Programmer and Breakfast”

How not to use Java key stores

I spent half a day today trying to see why a the web start application created and deployed using a simple build script didn’t work after I’ve created a new certificate as our previous was about to expire.

First I suspected it was because I signed the application using SUN JDK 1.6, and it might have some compatibility issues (as if) then I suspected it was the IBM JDK 1.5’s ikeytool I used to create the key (NB. I’m lazy to learn the keytool paramers, so I prefer to use a GUI for creating keys)

It turned out I was presuming the keys use UTF internally, like most Java applications should, so when entering the locality I used “Vác” with accented characters. Neither ikeytool, neither keytool warned me about this. Once creating a new key without the accent the application started working straight away.

Since “Budapest” doesn’t have any special characters, I never noticed this, but I think it deserves to be mentioned, so others won’t run into this problem.

DIY Push-puppet toy

Remember the push-puppets you had when you were a kid? These little puppets collapse instantly when you push the button on the base they stand, providing hours of quality entertainment to kids like myself. Now it has returned from exile and is back grouped up with new technology to provide hours of entertainment to IT people like myself.
Puppet

For the original concept see:Availabot

For the cuter Linux version visit Push puppet toy

I can imagine my desk full of these puppets each representing a friend I usually talk to, these puppets would dance up and down all day depending on who’s leaving for lunch.

Also it would be great to have this extended to have an intermediary stage, ie. when the button is half pushed these puppets usually start letting their head down. So when the contact is not offline, but only away they could show how said they are to be away from their computer, and when they are offline they are completely splattered.

The story behind a name..

I just came around the Winstone servlet container (related to the Hudson CI server). The project itself is fairly interesting, but it’s the naming choice which got me completely amazed..

Winstone is the name of a rather large Jamaican man a friend of mine met one night, while he was out clubbing in the Roppongi area of Tokyo. He (my friend) was a little liquored up at the time, and when Winstone suggested they head to “this really cool club” he knew, he didn’t think anything was wrong. It wasn’t until Winstone led him down a dark stairwell and dropped his trousers that my friend clued in and ran like hell.

It was too good a story to let die, so I named this project Winstone so that said friend will continue to be reminded of it.

Now who claims java devs should get a life? 🙂