package eecs1022.finala; /** * This question is worth 2.5 marks. No partial marks will be awarded for this question. * * @author Franck van Breugel */ public class Question6 { /** * Returns the sum of the digits occurring in the given string. You may assume that * the string is not null and not empty * * @param s a string. * @return the sum of the digits occurring in the given string. */ public static int sum(String s) { int sum = 0; for (int i = 0; i < s.length(); i++) { char letter = s.charAt(i); if (letter >= '0' && letter <= '9') { int number = letter - '0'; sum = sum + number; } } return sum; } /** * Tests method. You may add more test cases. * * @param args not applicable. */ public static void main(String[] args) { System.out.println(Question6.sum("1234")); System.out.println(Question6.sum("1a5h5x1")); System.out.println(Question6.sum("12h56s9")); System.out.println(Question6.sum("aaaaaa4aaaaa")); System.out.println(Question6.sum("5a")); } }