Wednesday, February 28, 2007

Java Case or Switch Example

How do you use the CASE function in Java?

The Switch or Case function in Java is useful for branching off to different actions based on a conditional expression. For example if you have a series of categories that can be selected and depending on which category was selected, you want the program to do a different action, the Case function is the best way to do this.

Here is the syntax

    switch( expression ) {
case value1:
statement1;
break;
case value2:
statement2;
break;
case value3:
statement3;
break;
...
case valuen:
statement n;
break;
default:
statements;
}


A real example of this from the Sun website:

int month = 8;
switch (month) {
case 1: System.out.println("January"); break;
case 2: System.out.println("February"); break;
case 3: System.out.println("March"); break;
case 4: System.out.println("April"); break;
case 5: System.out.println("May"); break;
case 6: System.out.println("June"); break;
case 7: System.out.println("July"); break;
case 8: System.out.println("August"); break;
case 9: System.out.println("September"); break;
case 10: System.out.println("October"); break;
case 11: System.out.println("November"); break;
case 12: System.out.println("December"); break;
default: System.out.println("Invalid month.");break;
}

Tuesday, February 27, 2007

String Parsing in Java

In Awk you can easily find the position of a delimiter and then take all characters up to that point by using the index() and substr() functions. But how does that work in Java? Is there an easy way to parse a String and carry out these type of routine actions?

Here you go:

subtring method:

int start = 1;
int end = 4;
String substr = "aString".substring(start, end); // Str
index method:
String string = "madam, i am Adam";
// First occurrence of a c
int index = string.indexOf('a'); // 1
Here is a live example I am working with where I need to parse the String into two variables as my webpage is passing me a parameter that is pipe-delimited:

String str = "YES|12345";

int pipe = str.indexOf('|'); //4

String firstPart = str.substring(1,pipe-1);
String lastPart = str.substring(pipe+1);

Oracle SQL DECODE

The DECODE function in Oracle SQL is used for if-then-else statements. A very handy tool for powerful queries.

The syntax for the DECODE function is as follows:

DECODE(value, if1, then1, if2, then2, if3, then3, etc., else)

Here is a good example of this from the devx.com website:

Consider the following example in which the Viewable column can have values of 1, 2, and 3:

SELECT FirstName, Viewable
FROM employee;

Results:

FIRSTNAME Viewable
===========================
John 1
Tim 2
Julie 2
Stacy 1
Rahul 3
Leena 4
Amy 1
Bill 3
Teri 3

Now, we can use Decode to display different things in a report based on the values in Viewable.

SELECT Firstname,
Decode(Viewable, 1,'VISIBLE', 2,'INVISIBLE',
3,'UNKNOWN', 'OTHER')
FROM employee;

Results:

FIRSTNAME Viewable
===========================
John VISIBLE
Tim INVISIBLE
Julie INVISIBLE
Stacy VISIBLE
Rahul UNKNOWN
Leena OTHER
Amy VISIBLE
Bill UNKNOWN
Teri UNKNOWN

Decode checks the column values and interprets the provided values in pairs. This is how it works:

Switch viewable:

Case 1:
Result = VISIBLE
Case 2:
Result = INVISIBLE
Case 3:
Result = UNKNOWN
Default:
Result = OTHER
End Case

OR
====================

If Viewable = 1 Then
Result = VISIBLE
Elsif Viewable = 2 Then
Result = INVISIBLE
Elsif Viewable = 3 Then
Result = UNKNOWN
Else
Result = OTHER
End If

One can use Decode in Updates as well. For example, instead of writing 50 different updates to change state names, we can do it in one Update using Decode as follows:

Update employee
set homestate = decode(homestate,
'AL', 'Alabama',
'AK', 'Alaska',
'AS', 'American Samoa',
'AZ', 'Arizona',
. . . . . . . .
. . . . . . . .
'WA', 'Washington',
'WV', 'West Virginia',
'WI', 'Wisconsin',
'WY', 'Wyoming' , homestate)
where homecountry = 'UNITED STATES'

This will replace state abbreviations with state names. And it leaves the abbreviations alone in case it doesn't match any of the values, such as AL, AK, etc.


Most Oracle SQL users do not realize the power of this function. Hopefully this post will enlighten some on it.

Sunday, February 25, 2007

Scientology Edinburgh


A new website was just put up for the Church of Scientology in Scotland. The Volunteer Ministers are really needed there right now with the recent train wreck.

Tuesday, February 20, 2007

Google Directory - Computers > Data Formats > Markup Languages > HTML > Tutorials

I figured its about time I learned HTML. Here is a great starting point with a plethora of tutorials, examples, etc.

Google Directory - Computers > Data Formats > Markup Languages > HTML > Tutorials

Submit Links Directory

Wednesday, February 14, 2007

Moving Data around HTML pages with JSP

Ok, I admit I never programmed a web site before. But I've recently realized the potential in using an internal website to handle administrative tasks such as tracking jobs, etc. Previously I tried doing such with a Swing based approach as this is what I was familiar with, but after consulting with a fellow programmer I chose to set up a webpage with hibernate on top of a MySql db. So far so good, however, I am looking for a crash course in HTML and JSP to get me going.

An excellent guide for this is on IBMs site under JSP Best practices. I am going through this and hopefully it will answer all my questions...

Event Viewer not showing up

What to do if the alerts in your Event Viewer do not show up? Try the refresh button in the Event Viewer. Usually this means there are too many events logged and your Event Viewer is jammed.

This happened to me after installing some new software that bunged up my machine.
Events were being logged like every minute or so and it shortly was overstuffed. I searched the net and found this suggestion in a TechRepublic forum and it worked. After refreshing I could see the events, delete the log if needed and carry on.