Subscribe to Alvin Poh's Blog by RSS reader
Subscribe to Alvin Poh's Blog by Email
Gadgets, Technology, Public speaking and IT from an undergraduate's perspective.
How to Learn and Use Regular Expressions (Regex)
Whoever would have thought that something that looks like gibberish would actually have such powerful usages. Look at this example:
\d\d[/]\d\d[/]\d\d[ ][-][ ]
Would you believe that it’s actually an expression that matches anything of the format of “12/06/08″? Amazing.
I’ve been putting off learning about regular expressions (regex) for the longest time because it seems overly complicated — but I was forced to learn more about it after a recent application demanded of it. And boy was I glad to have finally jumped into it — it’s simply one of the best ways to search for any text pattern.
With regular expressions, you can search for any number of characters, specify whether it’s a number or letter, and even specify a range, like only accept alphabets between B and E. Or only numbers from 1-5.
So if you think about it, the sample regular expression string up there can actually be improved upon. For instance, since the date format is dd/mm/yy, we know that dd can go higher than 31, nor can it go lower than 1. And similarly, we know that for mm, it’s between 01 to 12. The tricky part is yy, because it can vary, depending on how you’re going to use this date. If your application’s more geared towards the future, then keeping the first digit to 0 or 1, and the 2nd digit from 0 to 9 should be just fine.
So as you can see, if you’re a developer, then regular expressions (regex) should be something that you are very familiar and comfortable with, because it makes your life so much easier!
Subscribe to my blog:
RSS reader
Email
How To Resolve 500 Internal Server Error
The part about developing applications that I hate most is the troubleshooting part because it is the most tedious and the largest timesink. Unhelpful error messages and a lack of logs make it worse. If you develop applications then one of the first things you’d want to do is to have a proper logging system because only then will you be really able to track down what went wrong and fix it.
The worst kind of errors are those generic errors, which don’t even tell you what sort-of went wrong. No line numbers, no exception triggered in particular, etc. If you miss out a curly brace in JSP for instance, you get a try-catch exception error that will totally veer your troubleshooting efforts in the wrong direction unless you’re pretty clear with your code.
Speaking about web applications, the 500 Internal Server Error is a dreaded error message too.
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.Please contact the server administrator, webmaster@yourdomain.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.
If you get a 500 Internal Server Error, don’t go poking around your source code as yet. It might be something else, like a misconfigured web server, or simply the wrong file permissions, or the wrong web user or file owner. That’s what happened to me — I spent a good 15 minute or so looking through the file’s sourcecode before it hit me that it could be something else entirely.
I should have thought about that earlier because nothing changed, code-wise. It turned out that the folder that the script resided in had the wrong user/owner assigned. So I deleted that folder, and used FTP to reupload it, and viola — it works!
Subscribe to my blog:
RSS reader
Email
Formatting a Decimal Number to 2 Decimal Places in Java or JSP
I was looking around for the class to format a number to 2 decimal places while I was coding Java, or specifically JSP, and I found it.
First, you need to import the DecimalFormat class, so have this line at the top of your Java file:
import java.text.DecimalFormat;
Or if you’re running JSP:
<%@page import="java.text.DecimalFormat" %>
And here’s the actual code that does the magic:
double price = 10.59;
DecimalFormat priceFormatter = new DecimalFormat("$#0.00");
return priceFormatter.format(price);
That’s all there is to it! Simply change the Formatting string “$#0.00″ to anything you wish. The pound (#) sign simply means there can be optional digits there. The two zero’s (0) to the right of the decimal place means that it has only 2 decimal places, and will be replaced by 0s if you don’t have digits there. Te $ sign right at the start is something that I put in, because I wanted to display my prices in dollars ($).
So for example, you can also have the formatting string as “#0.00 seconds more” and it’ll show “2343.36 seconds more”.
Subscribe to my blog:
RSS reader
Email
Running UNIX commands in your Java application
I was looking around for a method to run commands from the prompt automatically from within my Java application, and then I found it. Here’s the solution - and it works like a charm!
static void doExec1() throws IOException {
// use pipes and I/O redirection
// but without using a shellRuntime runtime = Runtime.getRuntime();
runtime.exec(”ls | wc >out1″);
}
Subscribe to my blog:
RSS reader
Email
Replace/remove character in a String
To replace all occurences of a given character :
String.replaceAll("n", ""); // Remove all n
String.replaceAll("n", "r"); // Replace n by r
To replace a character at a specified position :
public static String replaceCharAt(String s, int pos, char c) {
return s.substring(0,pos) + c + s.substring(pos+1);
}
To remove a character :
public static String removeChar(String s, char c) {
String r = "";
for (int i = 0; i < s.length(); i ++) {
if (s.charAt(i) != c) r += s.charAt(i);
}
return r;
}
To remove a character at a specified position:
public static String removeCharAt(String s, int pos) {
return s.substring(0,pos)+s.substring(pos+1);
}
Subscribe to my blog:
RSS reader
Email
01 Feb 08 | 

