• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

is there an isDigit( ) method ?

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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");
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The java.lang.Character class has the various isDigit, etc methods.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic