Archive

Archive for the ‘Programming’ Category

How to run External Program in VBScript?

December 10th, 2008 No comments

In some case, you need to tell VBscript to open external program or open a certain file using external program.

Example below demonstrates how execute adobe reader (pdf reader)

OPTION EXPLICIT
 
Dim fso, ws, file, APP_PATH
Set ws = WScript.CreateObject("WScript.Shell")
 
APP_PATH = """C:\Program Files\AdobeReader 9.0\Reader\AcroRd32.exe"""
file = "C:\temptest.pdf"
 
ws.run APP_PATH & " " & file

To run this example:

  • Copy this code, past it in to new text file
  • Change value to correct path and file
  • Save it OpenFile.vbs
  • Double click on OpenFile.vbs

Or you might download source file vbs-openfile.vbs and make modification to it.

Advertisement » VBScript Programmer’s Reference by Adrian Kingsley-Hughes
Categories: Programming

A good practice to Compare String in Java

December 7th, 2008 No comments

Many people confused or don’t understand the concept to use String object properly.
In most case, when they try to compare 2 identical string objects they don’t get matching result.

When you have to do string comparison, there are few points to remember:

  • These operators <, <=, >, or >= cannot be used
  • These operator == and != don’t compare character in the string or string outside String pool. Only use these operator when you want to check if String value is NULL
  • Use String .equals , String .compareTo and Collator .compare to get accurate comparison result. Use these operators when String value is not NULL

The following class demonstrate how string are being compare in different way and the result of each

import java.text.Collator;
 
public class TestCompareObject {
 
  public static void main(String[] args) {
      String s1 = "Same String Value but could be different result";
      String s2 = "Same String Value but could be different result";
      String s3 = new String ("Same String Value but could be different result");
      Collator c = Collator.getInstance();
 
      System.out.println("1. s1 == s2 ? : " + (s1 == s2 ? "true" : "false"));
      System.out.println("2. s1 == s3 ? : " + (s1 == s3 ? "true" : "false"));
      System.out.println("2. s1.compareTo(s3) : " +
              (s1.compareTo(s3) == 0 ? "equal" : "not equal"));
      System.out.println("3. s1.equals(s3) : " +
              (s1.equals(s3) ? "true" : "false"));
 
      int c_compare = c.compare(s1, s3);
      System.out.println("4. Collator compare(s1, s3) ? : " +
              (c_compare == 0 ? "equal" : "not equal") );
  }
 
}

Below is the output of this program

1. s1 == s2 ? : true
2. s1 == s3 ? : false
2. s1.compareTo(s3) : equal
3. s1.equals(s3) : true
4. Collator compare(s1, s3) ? : equal
Categories: Programming

How to Read Text File in Java?

December 6th, 2008 No comments

Example below showing how to read text file

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
 
/**
 * Example class for Reading text file
 *
 * @author Manet Yim (manet.yim at gmail dot com)
 */
public class ReadTextFile {
 
    public void readTextFile() {
 
        // Create new file object
        File file = new File("C:\Temp\TestFolder\TestFile.txt");
 
        try {
            // create file reader instance
            FileReader fr = new FileReader(file);
            // create memory buffer to store data read from file
            BufferedReader br = new BufferedReader(fr);
            // variable to store text from file
            String line = "";
            while ( (line = br.readLine()) != null ){
                System.out.println(line);
            }
            // close file reader
            fr.close();
            // close buffer reader
            br.close();
 
        }
        // it is required to catch exception in case file is not found
        catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        // it is required to catch exception in case file cannot be read
        catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    public static void main(String[] args) {
        ReadFile app = new ReadFile();
        app.readTextFile();
    }
 
}
Categories: Programming

How to redirect page in HTML?

November 13th, 2008 No comments

Code below will demonstrate how to redirect from current page to a new page

<html>
<head>
<title></title>
<meta http-equiv="REFRESH" content="0;url=http://www.computer-faqs.com">
</head>
<body>
</body>
</html>

For demonstration, save this code in file.html then open it in a web browser, it will redirect the current page to the url specified in the meta tag.

Categories: FAQ & Tip, Programming

What are Primitive Data Types in Java Language?

September 8th, 2008 No comments

This is the question that might rise by the Java programming beginner. Well, the answer to this question is: Primitive Data Types are the type that is preliminary defined by the Language which normally known as Keywords or reserved words.

In Java Language there are 8 primitive data types supported.

  • byte: is an 8-bit signed two’s complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive).
    Default value: 0
  • short: is a 16-bit signed two’s complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive).
    Default value: 0
  • int: is a 32-bit signed two’s complement integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive).
    Default value: 0
  • long: is a 64-bit signed two’s complement integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive).
    Default value: 0L
  • float: is a single-precision 32-bit IEEE 754 floating point. The value range can be found here.
    Default value: 0.0f
  • double: The double data type is a double-precision 64-bit IEEE 754 floating point. The value range can be found here.
    Default value: 0.0d
  • boolean: has only two possible values: true and false.
    Default value: false
  • char: is a single 16-bit Unicode character. It has a minimum value of ‘\u0000′ (or 0) and a maximum value of ‘\uffff’ (or 65,535 inclusive).
    Default value: ‘\u0000′

Number and String are not primitive data type. They are java Object defined under java.lang. They come with subclass or methods for manipulating the value of the object itself.

Reference:

Categories: Programming

How to write One Line if-else statment in Java?

September 6th, 2008 No comments

Do you want to shorten your code? Below is a trick how to write one single line if-else statement.

Syntax

(condition) ? true-result : false-result

Where

  • condition: is your if condition
  • true-result: is executed when condition is true
  • false-result: is executed when condition is false

Example

System.out.println((1 + 2 == 4) ? "true" : "false");

This line of code will print “false” because the condition 1+2 not equals to 4

System.out.println((2 + 2 == 4) ? dothis() : dothat());

This line of code will dothis() method because the condition 2+2 equals to 4

Categories: Programming

Read and Write Properties file in Java

September 3rd, 2008 4 comments

Today, I wrote a simple class for reading properties file in Java Language. You could just use it without any need of further modification for a simple job like read and write value from and to properties file.

At the bottom of this class, there’s a main method for you to test-run to see the effect of the result.

    /**
     * Example starting method, demonstrate how PropertiesIO class is used
     * @param args
     */
    public static void main(String[] args){
        PropertiesIO pio = new PropertiesIO("info.properties");
        System.out.println(pio.readValue("variableone"));
        System.out.println(pio.readValue("variabletwo"));
        pio.writeProperty("newkey", "This is value of newkey");
    }

Read more…

Categories: Programming

VBA Auto Executor

August 24th, 2008 No comments

Sometimes when you need to execute a task every period of time and there’s no straight predefined function that you can just call and use with a few lines of codes. You will actually need to implement your own module.

I’ve written this nice class module sometime ago and I believe it will be useful for other VB/VBA developer out there. This class is an Auto Executor, it will automatically call and run a defined Routine.

It will solve the problem in the scenario above and it is written in an object oriented style which you can just embedded the whole class in your project and when you need to use it, just create the instance of it then call the public sub routine to perform the task.

Read more…

Categories: Programming

Send mail using SMTP in Java

August 14th, 2008 No comments

Not always but there is such time that you need to automate your email from within your application. When you write a program in Java language, you can sending mail by just using native JavaMail from Sun.

Read on, I am showing you how it is done and how less the code would be written.

Requirements

Starting Guide for Eclipse IDE (for an experienced eclipse user)

  • Create a project
  • Add one class call EmailPost.java
  • Copy and Paste code from this article
  • Add Javamail api library into your class path
  • Now run it as Java Application

Read more…

Categories: Programming

VBA XML Input/Output

July 29th, 2008 No comments

This article is for VBA developer. I’ve written this class to save my work in the future when I have to deal with data I/O. Hopefully it will be useful for you as well.

This class can be used as part of your VBA project. It enables the flexibility and control the format of data.

The main functionality is for data Input/Output. eg. Save/Write data to/from file. It is using Microsoft XML, v3.0 which should compatible with MS Office 2000 onward.

NOTE: This class module is not working with non-default UserForm Controls, you will need to modify it so it will recognise your Controls type

Features

  • Output Form data to a well-formed XML file format
  • Load data from XML file to Form and intelligently place data to correct field without deveoper interaction, and work seamlessly across any UserForm
  • Check version compatibility between Data file and Form
  • Run on Backup mode can be disabled/enabled on demand
  • Purge Unused files can be disabled/enabled on demand or make direct Call
  • Option to Delete file to Recycle bin. This will prevent accidental delete

Read more…

Categories: Programming