2013-11-26 96 views
1

在我的JSP页面中,我让用户输入他们的美元余额。 然后我在servlet中捕获到这个:String input = req.getParameter("balance");如何检查用户输入是否是双精度?

如何检查input变量是否为双精度?我知道如何使用ParseDouble,但我不想捕捉异常。相反,我想将自己的错误消息传递给JSP,以便用户在输入错误时可以看到它。

有人可以帮助我吗?

+0

的可能重复【JAVA:如何检查字符串是否可解析为双(http://stackoverflow.com/questions/3543729/java-how-to-check-that-a-string-is-parsable-to-a-double) –

+0

捕获NumberFormatException,并重新抛出自己的'MyJspUserException'。 – jn1kk

回答

3

您还可以创建这样的功能:

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; 
     } 
    } 
相关问题