Converting a String to int. Set the int to 0 if String is null

前端 未结 12 2125
孤街浪徒
孤街浪徒 2021-01-17 10:09

I have a function which saves Android data in sqlite but I have to convert the String data to an Integer.

Whenever the S

12条回答
  •  执念已碎
    2021-01-17 10:43

    String str = "...";
    // suppose str becomes null after some operation(s).
    int number = 0;
    try
    {
        if(str != null)
          number = Integer.parseInt(str);
    }
    catch (NumberFormatException e)
    {
        number = 0;
    }
    

提交回复
热议问题