Archive

Archive for the ‘Programming’ Category

Java: How to convert String to array?

February 11th, 2009

Sometime you get a string in a fixed format and you want individualise it and store them into array so you can manipulate it easier with your other functions.

For example,

String array = "one,two,three,four,five,six,seven,eight,nine";

will become

String[] words = {one,two,three,four,five,six,seven,eight,nine};

Below code is demonstrating how fixed format string can be broken down and stored into array.

Class: StringToArray.java
Read more...

Programming

How to copy text file in Java?

January 30th, 2009

Code below demonstrating how to copy data from one text file to another in Java.

This testing method, is calling FileCopier.copyFile(File, File) to copy content of file test1.css to file test2.css

    public static void main(String[] args) {
        FileCopier.copyFile(new File("D:/TEMP/test1.css"), 
                            new File("D:/TEMP/test2.css"));
    }

Class: FileCopier.java
Read more...

Programming , ,

Understand Java break and continue Statements

January 19th, 2009

Let's get it right and understand on usage of break and continue Statements in Java Programming Language.

break Statement

This statement causes iteration to stop/exit immediately, any line remained after this statement will be ignored by the JVM.

It is used in the following loop

  • for
  • while
  • do...while
  • switch

continue Statement

This statement skips the current iteration of the loop, any line after this statement will not be executed by the JVM until the condition is met.
Read more...

Programming ,

How to add or display favicon on a webpage?

January 17th, 2009

What is favicon?

Favicon is shortcut of favorites icon, also known as website icon, shortcut icon, url icon, or bookmark icon.

On the graphical web browser, this icon locates on the Address bar and Tabs bar as seen in the screenshot below.

Screenshot of favicon

Screenshot of favicon

Add favicon

You required to have an image of 16x16 pixels, either PNG, ICO or GIF format

To display favicon to your webpage, add one of the following html code inside <head> depends on which image format you have.

<head>
<link rel="icon" type="image/x-icon" href="path/myicon.ico">
</head>

<head>
<link rel="icon" type="image/png" href="path/myicon.png">
</head>

<head>
<link rel="icon" type="image/gif" href="path/myicon.gif">
</head>

Programming ,

Generate Random Password in Java

January 16th, 2009

This article demonstrates how to generate random password or string using java.util.Random.

You can define your own set of characters in this string variable

private static final String charset = "!0123456789abcdefghijklmnopqrstuvwxyz";

Class: RandomPassword.java
Read more...

Programming ,

Use Calendar instead of Date in Java

January 14th, 2009

Common Issue

When you use java.util.Date object with JDK version higher than 1.1

Date d = new Date();
d.getMonth();

You will get deprecated warning messages when you try to compile

The method getMonth() from the type Date is deprecated

However you can supress the warning message by adding @SuppressWarnings("deprecation") to you main() method

@SuppressWarnings("deprecation")
public static void main(String[] args) {
    ...
}

or adding @SuppressWarnings("unchecked") to other method

@SuppressWarnings("unchecked")
public void otherMethod() {
    ...
}

Note that this can only be used in JDK 5.0+

Alternative

Now let's forget about working around the deprecated method, let's learn how to use java.util.Calendar instead
Read more...

Programming , ,

What are Java Operators?

January 12th, 2009

In Java Language, there are 4 types of operators

  1. Arithmetic
  2. Relational
  3. Logical
  4. Bitwise

The rest of this post includes a small class to demonstrate on how those operators are used.

Arithmetic Operators

+  : Addition

-  : Subtraction

*  : Multiplication

/  : Division

%  : Modulus

++ : Increment

-− : Decrement

Read more...

Programming ,

How to include CSS Style Sheet file in HTML Document?

December 27th, 2008

Here is a quick tip. Code below showing you how to include CSS Style Sheet file in to HTML document

<html>
<head>
<title>Page Title</title>
<link type="text/css" rel="stylesheet" href="path_to/styles.css" />
</head> <body>
</body>
</html>

Programming

How to convert String to Date in SQL statement?

December 24th, 2008

This is a list of handy tips that could help you in a blink on how to convert String to Date in SQL. This post cover 5 popular databases (Oracle, MySQL, DB2, PostgreSQL, Informix)

Post your comment here if you find this post useful or want to contribute more explaination.

Oracle

Syntax:

to_date(date_string, format)

Example:

SELECT user.name, user.location FROM user
WHERE user.starteddtm = to_date('20/10/2007 16:01', 'dd/mm/yyyy hh24:mi');

Read more...

Programming , , , , , , ,

How to do Find and Replace in VBA?

December 24th, 2008

If you want to automate your find and replace in MS Office Word Document
You could use or modify this Sub to suit your own needs.

Remember it only support upto 512 bytes (512 characters) of text at the time.

Sub FindReplace()
' Find and replace string in document
' Support up to 512 bytes (double size the default)
  With Selection.Find
    .ClearFormatting
    .Text = "old text"
    .Replacement.ClearFormatting
    .Replacement.Text = "new text"
    .Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue
  End With
End Sub

Programming , ,