A problem with converting String to Int.
I have a ArrayList of strings. I need to convert them to integer if possible and compare to certain value witch is also integer. Here is what i do:
for(int j=0;j<sortList1.size();j++){
try {
iValue = Integer.parseInt(sortList1.get(j));
}
catch (NumberFormatException ex) {
}
if
(iValue==t) {t=t+1;}
The problem is that the code continues to work only till it didnt met any non convertable strings. When it meets something like "abc" it throws exception and doesnt check other elements from array. How can i solve this problem? Plz help!
Re: A problem with converting String to Int.
That cannot be ?! :(shake): Show us a full code example!
Re: A problem with converting String to Int.
Quote:
When it meets something like "abc" it throws exception and doesnt check other elements from array. How can i solve this problem?
Some how you need to detect if the String can be converted to an int. What do you want to do if it can't?
One way is to let the parseInt method do the testing and throw the exception which you catch and handle (for example just print a message and continue the loop to get the next item in the list).
Another would be to scan all the characters in the String and check if they are valid digits.
Re: A problem with converting String to Int.