I have a function which saves Android data in sqlite
but I have to convert the String
data to an Integer
.
Whenever the S
You can check for NumberFormatException
. Integer.parseInt()
will throw NumberFormatException
for cases:
String
is null
String
is empty (""
)String
cannot be converted to int
for any other reason (say String
is "aff"
or "1.25"
)Basically, it will handle all possible non-integer cases.
Code Example:
private static int convertStringToInt(String str){
int x = 0;
try{
x = Integer.parseInt(str);
}catch(NumberFormatException ex){
//TODO: LOG or HANDLE
}
return x;
}