title: Converting a String to int. Set the int to 0 if String is null
body: <p>I have a function which saves Android data in <code>sqlite</code> but I have to convert the <code>String</code> data to an <code>Integer</code>.</p>
  
  <p>Whenever the <code>String</code> is <code>null</code> i would like to save as <code>0</code></p>
  
  <p>The following is my code which fails whenever the value is <code>null</code></p>
  
  <pre><code> int block_id = Integer.parseInt(jsonarray.getJSONObject(i).getString("block_id"));
  </code></pre>
  
  <p>The <code>block_id</code> above is converted to an <code>Integer</code>.</p>
  
  <p>This is what i have decided to do but still it fails to convert the string value to <code>0</code> whenever its <code>null</code>.</p>
  
  <pre><code>int block_id = Converttoint(jsonarray.getJSONObject(i).getString("block_id"));
  </code></pre>
  
  <p>Then the function <code>convertToInt</code></p>
  
  <pre><code> public static Integer convertToInt(String str) {
      int n=0;
    if(str != null) {
        n = Integer.parseInt(str);
    }
      return n;
  }
  </code></pre>
  
  <p>How should I change it, to make it work?</p>