javabook
Class Convert

java.lang.Object
  |
  +--javabook.Convert

public class Convert
extends java.lang.Object

This class includes various methods for converting values from one data type to another data type. The advantage of using this class over the standard classes such as Integer, Double, and others is the consistency. Every method in this class follows the consistent form of "to", e.g., toInt, toFloat, and so forth.

This class is provided as freeware. You are free to use as provided or modify to your heart's content. But you use it at your own risk. No implied or explicit warranty is given.


Constructor Summary
Convert()
           
 
Method Summary
static boolean toBoolean(java.lang.String str)
          Converts the argument String object str to a boolean value.
static char toChar(java.lang.String str)
          Converts the argument String object str to a char value.
static double toDouble(java.lang.String str)
          Converts the argument String object str to a double value.
static float toFloat(java.lang.String str)
          Converts the argument String object str to a float value.
static int toInt(java.lang.String str)
          Converts the argument String object str to an int value.
static long toLong(java.lang.String str)
          Converts the argument String object str to a long value.
static java.lang.String toString(boolean bool)
          Converts the argument boolean value to a String value.
static java.lang.String toString(char ch)
          Converts the argument char value to a String value.
static java.lang.String toString(double number)
          Converts the argument double value to a String value.
static java.lang.String toString(long number)
          Converts the argument long value to a String value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Convert

public Convert()
Method Detail

toBoolean

public static boolean toBoolean(java.lang.String str)
Converts the argument String object str to a boolean value.
Parameters:
str - A String object to convert to a value (true or false) of data type boolean.
Returns:
a boolean equivalent of the passed argument
Throws:
java.lang.NumberFormatException - This exception is raised when the passed argument cannnot be converted to a boolean value. Example: "true" can be converted to a boolean value true, but "T" cannot be converted.

toChar

public static char toChar(java.lang.String str)
Converts the argument String object str to a char value.
Parameters:
str - A String object to convert to a value of data type char.
Returns:
A char equivalent of the passed argument.
Throws:
java.lang.NumberFormatException - This exception is raised when the passed argument cannnot be converted to a char value. Example: "Y" can be converted to a char value 'Y', but "AB" cannot be converted.

toDouble

public static double toDouble(java.lang.String str)
                       throws java.lang.NumberFormatException
Converts the argument String object str to a double value.
Parameters:
str - A String object to convert to a value of data type double.
Returns:
A double equivalent of the passed argument.
Throws:
java.lang.NumberFormatException - This exception is raised when the passed argument cannnot be converted to an int value. Example: "1234" can be converted to a double value 1234.0, but "ID345" cannot be converted.

toFloat

public static float toFloat(java.lang.String str)
                     throws java.lang.NumberFormatException
Converts the argument String object str to a float value.
Parameters:
str - A String object to convert to a value of data type float.
Returns:
A float equivalent of the passed argument.
Throws:
java.lang.NumberFormatException - This exception is raised when the passed argument cannnot be converted to an int value. Example: "1234.34" can be converted to a float value 1234.34f, but "A34.5" cannot be converted.

toInt

public static int toInt(java.lang.String str)
                 throws java.lang.NumberFormatException
Converts the argument String object str to an int value.
Parameters:
str - A String object to convert to a value of data type int.
Returns:
An int equivalent of the passed argument.
Throws:
java.lang.NumberFormatException - This exception is raised when the passed argument cannnot be converted to an int value. Example: "1234" can be converted to an int value 1234, but "45A" cannot be converted.

toLong

public static long toLong(java.lang.String str)
                   throws java.lang.NumberFormatException
Converts the argument String object str to a long value.
Parameters:
str - A String object to convert to a value of data type long.
Returns:
A long equivalent of the passed argument.
Throws:
java.lang.NumberFormatException - This exception is raised when the passed argument cannnot be converted to a long value. Example: "1234567" can be converted to a long value 1234567, but "45.98" cannot be converted.

toString

public static java.lang.String toString(boolean bool)
Converts the argument boolean value to a String value.
Parameters:
bool - A boolean value to convert to a String.
Returns:
A String equivalent of the passed argument.

toString

public static java.lang.String toString(char ch)
Converts the argument char value to a String value.
Parameters:
ch - A char value to convert to a String.
Returns:
A String equivalent of the passed argument.

toString

public static java.lang.String toString(double number)
Converts the argument double value to a String value. Use this method to convert a float value to a String value; a float is type compatible with a double.
Parameters:
number - A double value to convert to a String.
Returns:
A String equivalent of the passed argument.

toString

public static java.lang.String toString(long number)
Converts the argument long value to a String value. Use this method to convert an int, short, or byte value to a String value; they are type compatible with a long.
Parameters:
number - A long value to convert to a String.
Returns:
A String equivalent of the passed argument.