Converting values

Hi Is there any way to verify if a string value in a string variable can be converted into a integer variable ?Thank you
[149 byte] By [maazevedo] at [2007-9-30 21:46:14]
# 1
Integer.parseInt() and catch the NumberFormatException if it isn't an integer.Grant
ggainey at 2007-7-7 3:15:21 > top of Java-index,Administration Tools,Sun Connection...
# 2

That will certainly work, and it is probably the method I would adopt. There is one additonal possibility:

public boolean isNumber(final String target) {

char[] chars = target.toCharArray();

for (int current = 0; current < chars.length; current++) {

if (! Character.isDigit(chars[current]))

return false;

}

return true;

}

- Saish

"My karma ran over your dogma." - Anon

Saish at 2007-7-7 3:15:21 > top of Java-index,Administration Tools,Sun Connection...
# 3
Your additional solution doesn't really work because it needs to check to see if the value exceeds the integer size limit. It will work in situations where you sure you won't have that problem though.
bjon045 at 2007-7-7 3:15:21 > top of Java-index,Administration Tools,Sun Connection...
# 4
hi,In case if ur not dealing with floating numbers the better solution would be using matches methodpublic boolean IsNumber( String str) {return str.matches("[0-9]?");}I think this will solve any number in any rangeregardssivajik
sivaji_sun at 2007-7-7 3:15:21 > top of Java-index,Administration Tools,Sun Connection...
# 5
hi,sorry, I typed wronglycorrect one is ( should be one or more . so put + )public boolean IsNumber( String str) {return str.matches("[0-9]+");}I think this will solve any number in any rangeregardssivajik
sivaji_sun at 2007-7-7 3:15:21 > top of Java-index,Administration Tools,Sun Connection...
# 6

> Your additional solution doesn't really work because it needs to check to see if the value exceeds the integer size limit. It will work in situations where you sure you won't have that problem though.

Point taken. My example was mainly for illustrative purposes.

- Saish

"My karma ran over your dogma." - Anon

Saish at 2007-7-7 3:15:21 > top of Java-index,Administration Tools,Sun Connection...
# 7
Note that none of the new suggestions correctly handle negative numbers. (Although they could all be modified to do so fairly easily).Grant
ggainey at 2007-7-7 3:15:21 > top of Java-index,Administration Tools,Sun Connection...
# 8
I'm very happy with all sugestions and I really thank you. They make me thinking better about converting values. I'll see now wich will be better to my case. Thanks again
maazevedo at 2007-7-7 3:15:21 > top of Java-index,Administration Tools,Sun Connection...