title: Java: Adding Digits Of A String
body: <p>I have the correct code, I found an answer long ago, however I still don't understand why it works.</p>
  
  <pre><code>class Main {
      public static void main(String[] args) {
          System.out.println(D5PSum(10));
      }
  
      private static int D5PSum(int number) {
          String n = Integer.toString(number);
          int sum = 0;
          for (int i = 0; i &lt; n.length(); i++) {
              // (1) WHY DOES THIS NOT WORK
              //sum += Integer.parseInt(number.charAt(i));
  
              // (2) MORE IMPORTANTLY WHY DOES THIS WORK
              char c = n.charAt(i);
              sum += (c-'0');
              // (3) WHAT IN THE WORLD IS c-'0'
          }
          return sum;
      }
  }
  </code></pre>