Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

is there an isDigit( ) method ?

 
Shuaib Gill
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have an .htm file which has a form field that asks users to type in 10 numbers. Now if they type in anything that is not a number, I want to redirect them to an error page. I was wondering if there is an isDigit() function which tests to see if user input is a digit or is not a digit. Here is my .htm file and the servlet, SortItTest:
<html>
<head>
<title>
Bubble Sort Algorithm
</title>
</head>
<body>
<form name="bubblesort"action="http://mcs.ltu.edu/sgill/servlet/SortItTest" method="get">
<table>
<tr>
<td colspan = 5 align="middle">
Please enter 10 numbers:
</td>
</tr>
<tr>
<td>
<input type = "text" name = "number1" size="5">
</td>
<td>
<input type = "text" name = "number2" size="5">
</td>
<td>
<input type = "text" name = "number3" size="5">
</td>
<td>
<input type = "text" name = "number4" size="5">
</td>
<td>
<input type = "text" name = "number5" size="5">
</td>
</tr>

<tr>
<td>
<input type = "text" name = "number6" size="5">
</td>
<td>
<input type = "text" name = "number7" size="5">
</td>
<td>
<input type = "text" name = "number8" size="5">
</td>
<td>
<input type = "text" name = "number9" size="5">
</td>
<td>
<input type = "text" name = "number10" size="5">
</td>
</tr>
<tr>
<td colspan = 5 align="middle">
<input type = "submit" name = "sort" value="Sort !">
</td>
</tr>
</table>
</form>
</body>
</html>
// now here is some code from SortItTest.java
String number1 = req.getParameter("number1");
String number2 = req.getParameter("number2");
String number3 = req.getParameter("number3");
String number4 = req.getParameter("number4");
String number5 = req.getParameter("number5");
String number6 = req.getParameter("number6");
String number7 = req.getParameter("number7");
String number8 = req.getParameter("number8");
String number9 = req.getParameter("number9");
String number10 = req.getParameter("number10");
 
Amir Kamran
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!
If u want to restrict user to type just numbers than javascript is the best choice.
Well u can also do this:
try{
int n1 = Integer.parseInt(number1);
...
...
int n10 = Integer.parseInt(number10);

}catch(NumberFormatException e){
//redirecting code here
}
If in a string there is not a number an exception is raised and if all r numbers the numbers stored in integers.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The java.lang.Character class has the various isDigit, etc methods.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic