File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes is there an isDigit( ) method ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "is there an isDigit( ) method ?" Watch "is there an isDigit( ) method ?" New topic
Author

is there an isDigit( ) method ?

Shuaib Gill
Ranch Hand

Joined: May 29, 2001
Posts: 62
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");


programmer77
Amir Kamran
Ranch Hand

Joined: Apr 21, 2002
Posts: 48
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

Joined: Mar 22, 2000
Posts: 12681
    
    5
The java.lang.Character class has the various isDigit, etc methods.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: is there an isDigit( ) method ?
 
Similar Threads
Struts IE render problem
Applet Troubles
problem in jsp !!!
How to get a scriptlet value for javascript validation
My bean doesn't quite work