Home » Archive

Articles Archive for February 2009

Java Codes »

[28 Feb 2009 | No Comment | 46 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 | 29 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();    }}

Java Codes »

[25 Feb 2009 | No Comment | 97 views]

Use the static method parseInt of the Integer class to converta String to an int as below:
   public void convertStringToInt() {        String sValue = “5″;    int iValue = 0;        try {      iValue = Integer.parseInt(sValue);    }    catch (NumberFormatException ex) {      System.out.println(“The String does not contain a parsable integer”);    }        //Additional code goes here…  }

Alternatively you can pass a String to the Integer class constructorand call the intValue-method of the object:

  public void convertStringToInt() {        String sValue = “5″;    int iValue;        try {      iValue = new Integer(sValue).intValue();    }    catch (NumberFormatException ex) {      System.out.println(“The String does not contain a parsable integer”);    }    //Additional code goes here…  } 

Java Codes »

[25 Feb 2009 | No Comment | 46 views]

This code example shows how to convert an int value to a String object.Actually there are a few possible ways of doing this and three of the most common methods of doing so is presented here.First we use the static method toString() of the Integer class which takes an integer as argument and returns a String.The second way of converting from int to String is to instantiate the Integer class and pass the int value to the constructor, and then call the toString() method on the object.Finally the easiest way …

Java Codes »

[25 Feb 2009 | No Comment | 112 views]

This example shows how to convert a String to a byte. The String can only contain digits (or start with the – sign) and the number has to be within the byte range, otherwise a NumberFormatException will be thrown.The output from the code below is:65Tried to convert an invalid value
/** * * @author javabout.com */public class Main {        /**     * Example method for converting a String to a byte     */    public void convertStringToByte() {                try {            String s = “65″;                        byte b = Byte.valueOf(s);                        System.out.println(b);                        //Causes a NumberFormatException since the value is out of range            System.out.println(Byte.valueOf(“129″));                    } catch (NumberFormatException ex) {            System.out.println(“Tried to convert an …

Java Codes »

[25 Feb 2009 | No Comment | 86 views]

This example shows how to do a String to character array conversion. The String class has a neat method for doing this, toCharArray().In the code example below the String is converted to a char array and each element of the array is printed out.
/** * Main.java * * @author www.javabout.com */public class Main {        /**     * Converts a String to a character array     */    public void convertStringToCharArray() {                String str = “Abcdefg”;        char[] cArray = str.toCharArray();                for (char c : cArray)            System.out.println(c);    }    /**     * Starts the program     *     * @param args the command line arguments     */    public static void main(String[] args) {        new Main().convertStringToCharArray();    }}

The output from the executed code:

Abcdefg

Java Codes »

[25 Feb 2009 | No Comment | 78 views]

This java code example shows how to convert an array of characters to a String.To accomplish this we can utilize one of the String class many contructors, which takes an array of characters as parameter.
/** * Main.java * * @author www.javabout.com */public class Main {        /**     * Converts an array of characters to a String     */    public void convertCharArrayToString() {                char[] charArray = new char[] {‘a’, ‘b’, ‘c’};        String str = new String(charArray);                System.out.println(str);    }    /**     * Starts the program     *     * @param args the command line arguments     */    public static void main(String[] args) {        new Main().convertCharArrayToString();    }}

When execute the code will print out the created String.

abc

Java Codes »

[25 Feb 2009 | No Comment | 65 views]

This example shows three ways of converting a variable of datatype byte to a String. The first example uses the wrapper class Byte and it’s static method toString to convert the byte.The second example just concatenates the byte with an empty String, and the value is automatically casted to a String.The final example creates a byte array which is passed to the constructor of the String class.The output of the code below is:6565A
/** * * @author javabout.com */public class Main {        /**     * Example method for converting a byte to a String.     */    public void convertByteToString() {                byte …

Java Codes »

[25 Feb 2009 | No Comment | 66 views]

To convert a boolean value to a String you need to use the wrapper class of the boolean datatype. It has the same name as the datatype boolean, but it begins with the captial B – Boolean.To convert a boolean value you create an instance of the wrapper class sending the boolean value as argument to the constructor.Then you simply call the toString() method which will return a String representation of the boolean value.
/** * Main.java * * *#64;*#97;u*#116;*#104;or *#119;*#119;*#119;.j*#97;v*#97;*#100;*#98;*#46;*#99;o*#109; */public class Main {        /**     * Boolean to String conversion     */    public void convertBooleanToString() {                boolean theValue = true;                //Do the …

Java Codes »

[25 Feb 2009 | No Comment | 172 views]

There’s actually two ways to do a string to boolean conversion. Either you can create an instance of the wrapper class Boolean with a string value as argument to the constructor and call the method booleanValue(), or you can do as in the example below:call the static method parseBoolean() of the wrapper class to convert the string value to a boolean.Either way, the string value sent in has to be either ‘true’ or ‘TRUE’ to result in a boolean with the value of true, but it doesn’t have to be …