Java: removing the non-numbers characters from a string.

This is one of those code warrior snippets with two-lines that one never seems to find around the Internet, so I'm posting here for reference.

If you have a String and want to remove all the characters inside the string that are not numbers, this works:

public static int justNumbers(String input){
         String temp = input.replaceAll("[^0-9]""");
         return Integer.parseInt(temp);
     }

 Seems pretty simple doesn't it? Now look around the Internet and see the other techniques being proposed.

Hope this helps you.

TripleCheck, Beanshell and SPDX

I've been using most of my free time to develop the new SPDX tool. It started as command line tool and then moved up to a desktop edition. 

Not happy with the usual static forms that you see on normal GUI applications, I decided to add a flavor of HTML inside the Swing containers. The result was great. Then, I wanted to make sure that other people could change the code. It is no fun when you always need to compile things around when all you want is to just change a few settings.

Beanshell was used for scripting the plugins. Editing Beanshell scripts is not so much fun when programming errors happen. There is no syntax checking, no assistance to write code in the same smart manner as you see in modern IDEs available today.

The answer was creating what I call of Java-like beanshell. Moved things up to a state when you can comfortably write a Beanshell script exactly in the same fashion as it was a normal Java file. This was one heck of a productivity boost. Suddenly, writing dynamic scripts was much easier thanks to this neat trick.

Then came the next step, what about the web? I had been avoiding the idea of writing a web application. Basically because it places some restrictions such as requiring users to open a network port on their machines, which might not be possible on the case of many people working on machines at their companies.

Solution? Do a bridge between desktop and browser world. Today I completed this goal. The screenshot below demonstrates how it is possible to write java-like beanshell scripts that will behave exactly the same, regardless of the user access the program from the desktop or web edition.


 


From a programming point of view, this makes life quite simple. Imagine this thing like a PHP meets Java and Ruby on Rails altogether.

It is quite a mix. The best of the Java infrastructure (OS agnostic, robust language syntax, huge repository of available libraries and connectivity), the best of Beanshell (scripting just like PHP) and the best of two user interfaces: Desktop and Web Browsers.

Inside the beanshell script, a method is declared with the object that contains all the details about the request. Writing a plugin for the log viewer was a simple as this:

    /**
     * Displays a basic log of what has been happening
     * @param request The request from the end-user
     */

    public void showPage(WebRequest request){ 
       // get all the messages since the beginning
       String result = log.getMessagesSince(0);
       // replace the break lines with an HTML break line
       result = result.replace("\n", html.br);
       // write everything for the user to read
       request.setAnswer(result);
    }

Really simple, really fun.

I'm doing this code for the TripleCheck software that generates SPDX documents. If there is interest, I can later release this particular part of the code as project of its own for those who are interested in:

- Writing apps that work both on a Swing or Web browser
- Writing apps based on a Java-like scripting language that can be written from a normal IDE
- Keeping things simple and small (the whole software is less than 10Mb)

:-)