Here you can find the source of sumDigits(String number)
Parameter | Description |
---|---|
number | a parameter |
public static int sumDigits(String number)
//package com.java2s; //License from project: Apache License public class Main { /**//from www . ja va2 s . co m * Sums the digits of a natural number represented as a String. * * @param number * @return */ public static int sumDigits(String number) { return number.chars().map(aChar -> Character.getNumericValue(aChar)).sum(); /* * int sum = 0; for (int i = 0; i < number.length(); i++) { sum += * Integer.valueOf(number.substring(i, i + 1)); } * * return sum; */ } }