In addition to what Dave and wullxz said, you could also user regular expressions to find out if tested string matches your format e.g.
import java.util.regex.Pattern;
...
String value = "23423423";
if(Pattern.matches("^\\d+$", value)) {
return Integer.valueOf(value);
}
Using regular expression you could also recover other type of numbers like doubles e.g.
String value = "23423423.33";
if(Pattern.matches("^\\d+$", value)) {
System.out.println(Integer.valueOf(value));
}
else if(Pattern.matches("^\\d+\\.\\d+$", value)) {
System.out.println(Double.valueOf(value));
}
I hope that will help to solve your problem.
EDIT
Also, as suggested by wullxz, you could use Integer.parseInt(String)
instead of Integer.valueOf(String)
. parseInt
returns int
whereas valueOf
returns Integer
instance. From performance point of view parseInt
is recommended.