Write a java program to find the sum of the odd digit in a string.

Write a program to input a String array. The input may contain digits and alphabets (“de5g4G7R”). Extract odd digits from each string and find the sum and print the output.

For example, if the string is “AKj375A” then take 3+7+5=15 and not as 375 as the digit.

Assume maximum length of the array is 20.

Input and Output Format:

Input consists of an integer n, corresponds to the number of strings, followed by n Strings.

The output consists of an Integer.

Refer sample output for formatting specifications.

Sample Input :

3

cram2nisumt1

al33k

d2t4H3r5

Sample Output :

15

(1+3+3+3+5)

Find the sum of the odd digit in a string in Java.

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int s1 = sc.nextInt();
    String[] s2 = new String[s1];
    for (int i = 0; i < s1; i++) {
      s2[i] = sc.next();
    }
    System.out.println(getSum(s2));
  }

  public static int getSum(String[] s1) {
    int sum = 0;
    for (int i = 0; i < s1.length; i++)
      for (int j = 0; j < s1[i].length(); j++) {
        char c = s1[i].charAt(j);
        if (Character.isDigit(c)) {
          if (c % 2 != 0) {
            String t = String.valueOf(c);
            int n = Integer.parseInt(t);
            sum = sum + n;
          }
        }
      }
    return sum;
  }
}

Add a Comment

Your email address will not be published. Required fields are marked *

5 × one =