2

In my JSP page, I let users input their dollar balance. Then I catch this in servlet as: String input = req.getParameter("balance");

How do I check if input variable is double? I know how to use ParseDouble, but I don't want to catch the exception. Instead, I want to pass my own error message to JSP so that users can see it when they have typing error.

Can someone help me?

user3014926
  • 1,061
  • 7
  • 18
  • 26
  • possible duplicate of [Java: how to check that a string is parsable to a double?](http://stackoverflow.com/questions/3543729/java-how-to-check-that-a-string-is-parsable-to-a-double) – Martijn Courteaux Nov 26 '13 at 18:43
  • Catch the NumberFormatException, and rethrow your own 'MyJspUserException'. – jn1kk Nov 26 '13 at 18:45

1 Answers1

4

You may also create a function like this:

boolean isDouble(String input) {
        try {
            Double.parseDouble(input);
            return true;
        } catch (NumberFormatException e) {
            //You can send the message which you want to show to your users here.
            return false;
        }
    }
JamesB
  • 7,774
  • 2
  • 22
  • 21
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331