I want to read a number as a String, and split its characters to an integer array, and find the sum of it's digits by looping through that integers array.

This is my code so far:

```
public static void main(String[] args) {
    Scanner S = new Scanner(System.in);
    String Number = S.next();
    int counterEnd = Number.length();
    int sum = 0 ;

    for ( int i = 0 ; i < counterEnd ; i++) {
         sum += sum + (Number.charAt(i));
    }

    System.out.println(sum);
}

```

Unfortunately, this code prints the sum of ASCII not the digits.
['https://Stackoverflow.com/questions/28630360', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3301836/']
You can subtract the `'0'` character (i.e. `'1' - '0'` is `49 - 48` = `1`):

```
sum += Number.charAt(i) - '0';

```
You can do it like this:

```
   public static void main(String[] args) {
     Scanner S = new Scanner(System.in);
     String n = S.next();
     int sum = 0;

     for (int i = 0; i < n.length(); i++) {
       sum += Integer.parseInt(n.substring(i, i+1));
     }

     System.out.println(sum);
   }

```

Note: Replacing the body of the for loop with:

```
int offset = (n.substring(i, i+1).equals("-")) ? 1 : 0;
sum += Integer.parseInt(n.substring(i, i+1+offset));
i+=offset;

```

Will allow the program to take negative numbers. Ex: Inputting

```
-24

```

Would return a positive 2.