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
posted
0
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
posted
0
Thanks for posting that link. I am testingthis 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
posted
0
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
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
posted
0
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 ]