Home » Archive

Articles Archive for February 2009

Java Codes »

[28 Feb 2009 | No Comment | 322 views]

The class ‘Float’ is a wrapper class for the datatype float, and it has two static variables that contains the minimum and maximum values of the float datatype: MIN_VALUE and MAX_VALUE.You can print out the min and max values of the float datatype by specifying these variables as arguments to System.out.
/* * Main.java * * @author www.javabout.com */public class Main {        /*     * This method displays the minimum and maximum values of the datatype float.     */        public void displayMinAndMaxValuesOfFloat() {                System.out.println(Float.MIN_VALUE);        System.out.println(Float.MAX_VALUE);    }            /**     * @param args the command line arguments     */    public static void main(String[] args) {        new Main().displayMinAndMaxValuesOfFloat();    }}

The output from the executed code will …

Java Codes »

[28 Feb 2009 | No Comment | 158 views]

Often it is necessary to validate input data. One such validation could be to check if a user has entered a sequence of numbers.This example shows how you can check a String to see if it only contains integers. This validation could be done with less code if we were using regular expressions, but this example aims to keep it simple and more understandable so we use the Character wrapper class instead.The Character class has a static method called isDigit() which takes a char value as argument and returns true …

Java Codes »

[28 Feb 2009 | No Comment | 154 views]

If you want to change the location to where the standard output messages go there’s a method in the System class called setOut() which takes a PrintStream instance as argument.In this code example we show how to redirect all standard output messages to a file by creating a FileOutputStream instance and pass it to the PrintStream instance that is sent to the setOut method, like so:
import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.PrintStream;/** * Main.java * * @author www.javabout.com */public class Main {        /**     * Redirects System.out and sends all data to a file.     *     */    public void redirectSystemOut() {                try {                        System.setOut(new PrintStream(new FileOutputStream(“system_out.txt”)));                    } …

Java Codes »

[28 Feb 2009 | No Comment | 162 views]

To display the number of processors available to a certain Java Virtual Machine there is a method of the Runtime class called availableProcessors().To get an instance of the Runtime class we have to call the static method getRuntime(). The example below illustrates how to go about displaying the number of processors.
/** * Main.java * * @author www.javabout.com */public class Main {        /**     * Displays the number of processors available in the Java Virtual Machine     */    public void displayAvailableProcessors() {                Runtime runtime = Runtime.getRuntime();                int nrOfProcessors = runtime.availableProcessors();                System.out.println(“Number of processors available to the Java Virtual Machine: “ nrOfProcessors);            }        /**     * Starts …

Java Codes »

[28 Feb 2009 | No Comment | 79 views]

This example shows how to create a singleton object. A singleton is a class of which there can only be one instance in the same Java Virtual Machine.To create a singleton there has to be a private constructor because the class will itself control the one and only instance that will be created, and of course a private constructor cannot be called from outside the class.Instead, a method is created with public access that returns the singleton instance (if the method is called the first time the object is instantiated). …

Java Codes »

[28 Feb 2009 | No Comment | 39 views]

When comparing strings in Java you can either use the compareTo() method which the String class implements through the Comparable interface, or the equals() method which is declared in the Object class.Since the String class is derived from the Object class (like every class is) any of the methods can be used when working with strings in Java.In this code example we use the equals() method. Note that the method is case sensitive so if you want to make a case insensitive comparison you’ll need to use the method equalsIgnoreCase().
/** * …

Java Codes »

[28 Feb 2009 | No Comment | 35 views]

To replace a character in a string with another character you need to call the method replace() with the two characters as arguments.The first argument is the character to be replaced and the second is the character that will be inserted.
/** * Main.java * * @a*#117;*#116;ho*#114; www*#46;ja*#118;*#97;*#100;*#98;.*#99;o*#109; */public class Main {        /**     * This method replaces characters in a string     */    public void replaceCharacters() {                String str = “aaa bbb ccc”;                str = str.replace(‘b’, ‘d’);                System.out.println(str);    }        /**     * Starts the program     *     * @param args the command line arguments     */    public static void main(String[] args) {        new Main().replaceCharacters();    }}

When the code above is executed it will display:

aaa ddd ccc

Java Codes »

[28 Feb 2009 | No Comment | 36 views]

>This code example shows how to format a String to a Date object. The method convertStringToDate actually takes three arguments as integers, date, month and year, and then concatenates these values to a String date.The String date is them passed to the parse method of the SimpleDateFormat object that returns a java.util.Date object. The SimpleDateFormat class takes a String format as input to the constructor.In case the format fails a ParseException is thrown.
>import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;/** * * @auth*#111;*#114; www*#46;*#106;a*#118;adb*#46;*#99;*#111;m */public class Main {        public void convertStringToDate(int date, int month, int year) {                SimpleDateFormat dateFormat …

Java Codes »

[28 Feb 2009 | No Comment | 45 views]

This example shows how to count letters in a String. We use the Character wrapper class though it could as easily be done with regular expressions.We simply call the static method isLetter() for each character in the String. This method returns true if it’s a letter and false if otherwise. In the example below we use both letters and numbers in the input parameter and the isLetter method will of course return false for the numbers.The output from the code below will be:‘The input parameter contained 5 letters.’
/** * * @author javabout.com */public …

Java Codes »

[28 Feb 2009 | No Comment | 82 views]

This example shows how to get the value of the current directory.It gets the value of a system property called “user.dir”;
public class FileUtil {  public void displayCurrentDir() {    System.out.println(System.getProperty(“user.dir”));        }  public static void main(String[] args) {    FileUtil fileutil = new FileUtil();    fileutil.displayCurrentDir();  }}