Here is old article ( we are using JSTL 1.0 ).
http://java.sun.com/develo
Main Topics
Browse All TopicsI need to know if a variable contains a number or not.
I have some conditional formatiing that is taking place, and the variable could either be set to nothing, null, or a number. Is there a JSP function like this?
String myvar = null
myvar = request.getParameter("myva
if(myvar.IsNumber()){
//Do Something
}else{
//Do Something else...
}
Thanks in advance,
-MD
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Here is old article ( we are using JSTL 1.0 ).
http://java.sun.com/develo
No, but this will:
public static boolean isNumber(String in) {
if (null == in || in.trim().length() == 0) return false;
return in.matches("\\d*(?:\\.?\\d
}
This allows any number. If the variable contains a decimal it must have at least 1 number following it. Here are some example results:
.321 = true
2.45 = true
23.1 = true
23. = false
230 = true
If the OP specifies his exact requirements (like whether he has to handle cases like your last example) then the proper regex can be built to handle that. I disagree that rrz's method is the best way to do it. Checking whether something is true or not by determining whether an exception is thrown is a bad design pattern. I was also going to back this argument up by mentioning that it would be more expensive to test this way but I was suprised by the results.
Running this code:
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class FindNumber {
private static final Pattern p = Pattern.compile("\\d*(?:\\
public static void main(String[] args) {
double start, end;
// Use regex, isNumber1()
start = System.currentTimeMillis()
for (int i=0; i<1000000; i++) {
isNumber1("55.55");
}
end = System.currentTimeMillis()
System.out.println("Method
// Use exception check, isNumber2()
start = System.currentTimeMillis()
for (int i=0; i<1000000; i++) {
isNumber2("55.55");
}
end = System.currentTimeMillis()
System.out.println("Method
// Use regex, with regex pattern precompiled, isNumber3()
start = System.currentTimeMillis()
for (int i=0; i<1000000; i++) {
isNumber3("55.55");
}
end = System.currentTimeMillis()
System.out.println("Method
}
public static boolean isNumber1(String in) {
if (null == in || in.trim().length() == 0) return false;
return in.matches("\\d*(?:\\.?\\d
}
public static boolean isNumber2(String in) {
try {
float value = Float.parseFloat(in);
return true;
} catch (NumberFormatException ignored) {
return false;
}
}
public static boolean isNumber3(String in) {
if (null == in || in.trim().length() == 0) return false;
Matcher m = p.matcher(in);
return m.matches();
}
}
Resulted in these benchmarks:
Method using regex completed in 5.579 seconds.
Method using exception check completed in 0.593 seconds.
Method using regex with regex pattern precompiled completed in 1.641 seconds.
So my last argument is out the window :P
Whatever the OP chooses to use is up to him. It seems wrong to me to use isNumber2() but the benchmarks don't lie! I really expected the regex method to be much faster. Either way, I found this topic interesting and learned something new today :)
-Pat
Those results were bugging me today so I thought about it more and realized what made the exception check method so much faster. The test was with a valid float so the isNumber2() method never once threw an exception. If you use a value for the test that isn't a valid float, the benchmarks show a whole different picture:
Method using regex completed in 5.796 seconds.
Method using exception check completed in 9.125 seconds.
Method using regex with regex pattern precompiled completed in 1.891 seconds.
That makes isNumber3() look a whole lot better :)
-Pat
>Is there a JSP function like this?
There probably should be one.
Javascript has isNaN() .
Why not validate before submission ?
Here is another way.
public static boolean isNumber(String var){
int pointCount = 0;
for(int i=0;i<var.length();i++){
char c = var.charAt(i);
if(var.charAt(i) == '.')pointCount++;
if(!Character.isDigit(c)||
}
return true;
}
Business Accounts
Answer for Membership
by: rrz@871311Posted on 2004-08-25 at 18:43:05ID: 11898940
Did you try that code ? won't it throw a null pointer ?
r");
String myvar = null
myvar = request.getParameter("myva
if(myvar != null && myvar.IsNumber()){
//Do Something
}else{
//Do Something else...
}
Maybe you should look at JSTL if you are looking for functions. rrz