Workshop 2007 JRE Java Support
Installation
Home
CalcTool
Java Applets
Friends/Family
Gardening
Woodworking
Forums

The Convert Class

 

Download the Convert class here. JavaDocs are here.

 

The Convert class is useful for converting some data types to other data types.  When writing applets, I’ve primarily used the toDouble() and toInt() methods.  Syntax for use is

Convert.toType(argmument);

For example, when converting a String named inputString to a double, we would call the method as follows.

Convert.toDouble(inputString);

Currently available methods are summarized in the following table.

static public boolean

isDouble(String snum) Checks to see if snum is number with a double value. This is useful for error checking input.

static public long

isInt(String snum) Checks to see if snum is a number with an integer value. This is useful for error checking input.

static public double  

toDouble(int inum) Converts int to double

static public double[]  

toDouble(Object object) Converts Object to double[]

static public double

toDouble(String snum) Converts String to double

static public int  

toInt(double dnum) Truncates double to int

static public int  

toInt(String snum) Converts String to int

static public long  

toLong(String snum) Converts String to long

static public int  

toRoundedInt(double dnum) Rounds double to int

static public String  

toString(double dnum) Converts double to a String

static public String

toString(float fnum) Converts float to a String

static public String

toString(int inum) Converts int to a String

           

Useage

Here's an example of converting a double to a String:

double x=3.645;
String=stringx;
stringx=Convert.toString(x);

We can now output this using the setText() method.  For example:

label1.setText(stringx);

Here's an example of converting an int to a String:

int year;
String=stringyear;
stringyear=Convert.toString(year);

Here's an example of converting a float to a String:

float x=3.645;
String=stringx;
stringx=Convert.toString(x);

But the primary use of the Convert class is to convert strings to ints, doubles, etc.

Here’s an example of converting the string “3” into an int.

String string3=”3”;
int answer=Convert.toInt(string3);

answer will now have the integer value of 3.

 

Code in Convert.java: Using wrapper classes. 

I’ve used wrapper classes to construct the Convert.java class.  Read on if you are interested in the details…

 

Other types of objects that we may encounter are ones defined by Double, and Integer classes.  Note the capital letter at each of these classes.  The class Double is known as a wrapper class for the primitive type double.  Thus Double objects and double variables are two different things. The reason Java needs these classes is that it is an object-oriented language.  With each of these classes is defined a myriad of different methods that operate on the Double object.  But as you might expect there is a close relationship between an object defined by the Double class and a variable defined by the double primitive type.  Here’s an example of how to define a Double object.

Double dval=new Double(70.5);

This statement constructs a Double object named dval and gives it the value of 70.5.

Now suppose you want to extract the value of dval and assign it to the double variable height.  Here’s how to do this:

double height;
height=dval.doubleValue();

Or, you can combine these three statements into one:

double height=(new Double(70.5)).doubleValue();

You should observe that doubleValue() is one of many methods that will operate on a Double object.  There are parallel concepts  for Integer objects, Float objects, etc.