aspose file tools
The moose likes Java in General and the fly likes smart way to check that input is integer Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "smart way to check that input is integer " Watch "smart way to check that input is integer " New topic
Author

smart way to check that input is integer

Maha Hassan
Ranch Hand

Joined: Aug 02, 2005
Posts: 133
hi All
i need to find a way to check that the user input is a list of number ex[ 23 -34 -55 56 57 ]
only numbers , spaces and - are allowed
is there a smart way to solve this
thanks
Manuel Moons
Ranch Hand

Joined: Mar 05, 2002
Posts: 229
You can use a regular expression for this. Take a look at the "java.util.regex" package. The sun tutorial might be useful.
Sun Regular expression lessons
aven kimball
Greenhorn

Joined: Jun 23, 2005
Posts: 15
Thanks for posting that link.
I am testing this example. I have jdk 1.4.2_08. But it gave me the error package
java.util.regex do not exit. The tutorial also saying that
Before continuing with the next section, save and compile this code to ensure that your development environment supports the java.util.regex package. . So how to make it available?
Ali Hussain
Ranch Hand

Joined: Jun 19, 2005
Posts: 211
hi All
i need to find a way to check that the user input is a list of number ex[ 23 -34 -55 56 57 ]
only numbers , spaces and - are allowed
is there a smart way to solve this
thanks


You can use Character.isDigit method...


if (char = '-' | char = ' ' | Character.isDigit(char))...


- SCEA, SCJD, SCBCD, SCWCD, SCMAD, SCJP, ICAD (WebSphere), Lotus Principal CLP, Lotus CLP, Lotus CLS
Jean-Sebastien Abella
Ranch Hand

Joined: Jul 29, 2005
Posts: 60
String s = " 90";
int i = 0;
try
{
i = Integer.parseInt(s.trim());
}catch( NumberFormatException nbe)
{
//you know it's not ok!
}
fdafd fdafda
Greenhorn

Joined: Aug 09, 2005
Posts: 6
I think you want to check the input is a list of integers separated by space. Checking char one by one is not sufficient. it doesnt catch the case of [12 --13 14]
I would suggest you split the string into a string array and check them one by one.

the following method should return true for list [12 13 -14 15 -16] but false for [12 - 13 --14 15 16]

public boolean numList(String input)
{
String pattern= "[-]?[0-9]+";
String[] sLine=input.split(" ");
for(String sTemp: sLine)
{
if (!sTemp.matches(pattern)&&!(sTemp.equals("")))
return false;
}
return true;
}
[ August 09, 2005: Message edited by: Lucas Jiang ]
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: smart way to check that input is integer
 
Similar Threads
SMART way to match [0.997u -9.978u .98u -.987u ]
WA #1.....word association
Nettiquette
sql query
Take this test