2013-11-26 162 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; 
     } 
    } 
相關問題