Java Reference
In-Depth Information
4. Now you'll experiment with the numeric wrapper classes. Add this bolded
Java statement to the beginning of your HelloWorld source:
import java.text.*;
//
//
// HelloWorld Application
//
//
5. Add these Java statements at the end of the previous statement (before the
last two curly braces):
// Experiment with the numeric wrapper classes.
// Convert from String to several numeric types.
// You will use the static methods in the wrapper classes.
int i;
long l;
double d, d2;
String inputMsg = "003";
// Try to convert String to integer. Catch any NumberFormat
// exception errors.
try {
i = Integer.parseInt (inputMsg.trim());
}
catch (NumberFormatException e) {
i = 0;
}
// Try to convert String to a long. Catch any NumberFormat
// exception errors.
try {
l = Long.parseLong (inputMsg.trim());
}
catch (NumberFormatException e) {
l = 0;
}
// Try to convert String to a double. Catch any NumberFormat
// exception errors.
try {
d = new Double (inputMsg.trim()).doubleValue();
}
catch (NumberFormatException e) {
d = 0;
}
Search WWH ::




Custom Search