I realized that Java does not provide any standard library method like isInteger() for the verification.
I came upon two good methods for checking if the user has entered an integer value or float value.
Solution 1:
--------------------------------------------------------------
String enteredNumber = "6.234";
double d=double.parseInt(enteredNumber);
if (Math.floor(d)==d) {
// d is an integer
}
else
{
// d's not an integer
}
--------------------------------------------------------------
Solution 2:
--------------------------------------------------------------
String enteredNumber = "6.234";
try {
int v = Integer.parseInt(enteredNumber);
valid = true;
// it's an integer
} catch(NumberFormatException e) {
valid = false;
//it's not an integer
}
--------------------------------------------------------------
No comments:
Post a Comment