Sum Of Digits Of a String: This Java program achieves two tasks:
- Finding the sum of numbers in the given string:
- The program takes an input string containing alphanumeric characters and special characters.
- It extracts all the numeric values from the string and calculates their sum.
- For the input string “Welcome[21], Java1How are you78”, the output will be 100 (21 + 1 + 78).
- Finding the sum of digits in the given numbers:
- The program takes the same input string as above and extracts the numeric values.
- For each extracted number, it calculates the sum of its digits.
- The digits of the numbers are summed, and the final output is the sum of these digits.
- For the input string “Welcome[21], Java1How are you78”, the output will be 19 (2 + 1 + 1 + 7 + 8).
Sum Of Digits Of a String
package com.softwaretestingo.interviewprograms; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class InterviewPrograms46 { /* * 1. Find Sum of numbers in given strings. * Input : "Welcome[21], Java1How are you78" * OutPut : 100 [21+1+78] * * 2. Find Sum of digits in given strings. * Input : "Welcome[21], Java1How are you78" * OutPut : 19 [2+1+1+7+8] */ public static void main(String[] args) { InterviewPrograms46 main = new InterviewPrograms46(); String testData = "Welcome[21], Java1How are you78"; List<Integer> integers = main.getIntegers(testData); System.out.println("Sum of numbers : " + integers.stream().reduce(0, (n1, n2) -> n1 + n2)); System.out.println("Sum of digits of numbers : " + integers.stream().filter(num->num>0).map(num -> main.sumOfDigits(num)).reduce(0, (n1, n2) -> n1 + n2)); } private Integer sumOfDigits(Integer number) { int sum = 0; while (number > 0) { sum += number % 10; number = number / 10; } return sum; } private List<Integer> getIntegers(String testData) { return Arrays.asList(testData.split(" ")).stream().map(word -> getNumber(word)).collect(Collectors.toList()); } private Integer getNumber(String data) { String num = ""; try { for (Character ch : data.toCharArray()) { if (Character.isDigit(ch)) num += String.valueOf(ch); } } catch (Exception ex) { return 0; } return num.length() > 0 ? Integer.parseInt(num) : 0; } }
Output
Sum of numbers : 100 Sum of digits of numbers : 19
Let’s break down the program section by section:
- public static void main(String[] args): This is the entry point of the program. It initializes the InterviewPrograms46 class, defines the input string testData, and proceeds to perform the two tasks: finding the sum of numbers and the sum of digits.
- private Integer sumOfDigits(Integer number): This method calculates the sum of digits of a given integer number. It uses a while loop to extract the individual digits of the number and adds them to the variable sum. Finally, it returns the sum of the digits.
- private List<Integer> getIntegers(String testData): This method takes the input string testData, splits it into words using space as a delimiter, and then maps each word to an integer by calling the getNumber method on each word. The resulting integers are collected into a list using the collect method of the Stream API, and this list of integers is returned.
- private Integer getNumber(String data): This method takes a word data and extracts the numeric part from it. It iterates through each character of the word, and if the character is a digit, it appends it to the num string. Finally, it parses the num string to an integer using Integer.parseInt. If there are no digits in the word or an exception occurs during parsing, it returns 0.
The program effectively demonstrates how to extract numbers from a string and perform various operations on them using Java’s Stream API, making it a concise and efficient solution to the given problem.
Leave a Reply