0% found this document useful (0 votes)
22 views

Java Conversion

To convert a string to a number in Java, use the parse methods of the wrapper classes like Integer.parseInt(), Long.parseLong(), Float.parseFloat(), and Double.parseDouble(). These methods can throw a NumberFormatException if the string is null or not a valid number. To handle invalid input, wrap the conversion in a try-catch block and catch the NumberFormatException to notify the user and request valid input. The document provides an example utility method that uses a dialog box to repeatedly prompt the user for input until a valid integer is entered.

Uploaded by

dj_khalid
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Java Conversion

To convert a string to a number in Java, use the parse methods of the wrapper classes like Integer.parseInt(), Long.parseLong(), Float.parseFloat(), and Double.parseDouble(). These methods can throw a NumberFormatException if the string is null or not a valid number. To handle invalid input, wrap the conversion in a try-catch block and catch the NumberFormatException to notify the user and request valid input. The document provides an example utility method that uses a dialog box to repeatedly prompt the user for input until a valid integer is entered.

Uploaded by

dj_khalid
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 1

Java: Converting Strings to Numbers

To convert a string value to a number (for example, to convert the String value in a text field to an
int), use these methods. Assume the following declarations:

String s; int i; long l; float f; double d;


type Example statement

int i = Integer.parseInt(s);

long l = Long.parseLong(s);

float f = Float.parseFloat(s);

double d = Double.parseDouble(s);

If s is null or not a valid representation of a number of that type, these methods will throw
(generate) a NumberFormatException.

Handling NumberFormatExceptions

Put number conversions inside a try . . . catch statement so that you can do something if
bad input is entered. The conversion method will throw a NumberFormatException when there is
bad input. Catch the NumberFormatException, and do something to handle this error condition.
Put your conversion in the try clause, and the error handling in the catch clause. Here is an
example of the kind of utility function you might write to do this checking.

//--- Utility function to get int using a dialog.


public static int getInt(String mess) {
int val;
while (true) { // loop until we get a valid int
String s = JOptionPane.showInputDialog(null, mess);
try {
val = Integer.parseInt(s);
break; // exit loop with valid int >>>>>>>>>>>>>>>>>>>>>>
}catch (NumberFormatException nx) {
JOptionPane.showMessageDialog(null, "Enter valid integer");
}
}
return val;
}//end getInt

You might also like