Home » Archive

Articles tagged with: java language

Java Codes »

[28 Feb 2009 | No Comment | 49 views]

This example shows how to validate if a String is a number.The first example checks if it’s and Integer by trying to parse the String to an int.The second example can be used if you’re not sure if the possible number value contained in the String will exceed the maximum value for an int.Here we instead use the Long class to parse the String to the datatype long.
/** * Main.java * * @author www.javabout.com */public class Main {        /**     * Validates if input String is a number     */    public boolean checkIfNumber(String in) {                try {            Integer.parseInt(in);                } catch (NumberFormatException ex) {            return …

Java Codes »

[28 Feb 2009 | No Comment | 61 views]

To redirect standard error (or System.err) there’s a method in the System class called setErr() which takes a PrintStream instance as argument.If we for example want to redirect all messages on System.err to a file, we can create a FileOutputStream instance and send it to the constructor of the PrintStream.This Java code example creates the FileOutputStream with the filename “standard_err.txt” and pass it to the PrintStream, which in turn is passed to the setErr() method.A NullPointerException is forced to test that the stacktrace is printed to the file.
import java.io.FileNotFoundException;import java.io.FileOutputStream;import …

Java Codes »

[28 Feb 2009 | No Comment | 48 views]

This example shows how to clone objects by implementing the Cloneable interface. Some classes implements the Cloneable interface by default but when creating custom classes the interface needs to be implemented explicitly.The class that implements the Cloneable interface (the Person class in the example) needs to have a method named clone() that returns an object.In the example we have chosen to create a new instance of Person and populate its properties with the same values as the object that the clone() methodis called on.
/** * Main.java * * @author www.javabout.com */public class Main …

Java Codes »

[28 Feb 2009 | No Comment | 32 views]

To find the superclass of an object we first need to call the getClass() method of the object to return the Class-object.The Class object then has a method called getSuperClass() which returns another Class object on which the method getName() can be called.In the example below we call the getSuperClass() method for four different object types: Vector, ArrayList, String and Integer.
import java.util.ArrayList;import java.util.Vector;/** * Main.java * * *#64;a*#117;t*#104;*#111;*#114; www*#46;*#106;a*#118;*#97;d*#98;.c*#111;m */public class Main {        /**     * Constructor     */    public Main() {                checkObjectSuperClass(new Vector());        checkObjectSuperClass(new ArrayList());                checkObjectSuperClass(“Test String”);        checkObjectSuperClass(new Integer(1));            }        /**     * Checks which superclass the object has.     *     * @param testObject The object     */    public void checkObjectSuperClass(Object testObject) {                System.out.println(“Object …

Java Codes »

[28 Feb 2009 | No Comment | 72 views]

To check if a string contains another string the method indexOf() is used. It returns the index in the haystack string where the needle string begins.If the needle string is not present in the haystack string, the value -1 is returned.In the code example below we check two needle strings against a haystack string, one exists and one does not.
/** * Main.java * * *#64;a*#117;t*#104;*#111;r *#119;ww.*#106;a*#118;*#97;*#100;*#98;*#46;*#99;o*#109; */public class Main {        /**     * Checks if string contains another string.     */    public void checkStringContains() {                String haystack = “Programming in Java”;        String needle1 = “Java”;        String needle2 = “Pascal”;                int index1 = haystack.indexOf(needle1);        int index2 = …

Java Codes »

[28 Feb 2009 | No Comment | 54 views]

To convert all lower case characters in a string to upper case, use the method toUpperCase() of the String class.The example below shows how to use it. Note that the method returns a String so you’ll need to assign the return value of the method to a variable (in this case the same variable).
/** * Main.java * * *#64;*#97;uth*#111;*#114; www.*#106;a*#118;ad*#98;.c*#111;m */public class Main {        /**     * Converts all lower case characters to upper case     */    public void stringToUpperCase() {                String str = “All Lower Case Characters In This String Will Be Converted To Upper Case”;                str = str.toUpperCase();                System.out.println(str);    }        /**     * Starts the …

Java Codes »

[28 Feb 2009 | No Comment | 37 views]

>This code example shows how to display the current user logged on to the computer.There is a system property called ‘user.name’ which holds the value of the current user. The property is obtained by calling the getProperty() method of the System class.
>/** * * @*#97;u*#116;hor w*#119;*#119;*#46;*#106;av*#97;d*#98;.*#99;*#111;m */public class Main {        public void getCurrentUser() {                String currentUser = System.getProperty(“user.name”);        System.out.println(“Current user is “ currentUser);    }    /**     * @param args the command line arguments     */    public static void main(String[] args) {        new Main().getCurrentUser();    }}