How to find the Sum of Digits in an Alphanumeric value ?

This is the complete solution to find the sum of digits in an Alphanumeric value. This solution is designed to to solve the problem using an interface, super class and two derived classes implementing the interface.

package pack1;

public interface AlphanumericIntf {

	/* Define the functional interface with SAM 'ParseAlphanumeric' */
	float ParseAlphanumeric(String str1);

	/* Define the static method 'displayMsg' */
	static void displayMsg(String inpSen, float sumVal) {
		
		System.out.println("The sum of digits in the given string '" + inpSen + "' is " + sumVal);
		System.out.println("\n");
	}
}
package pack1;

import java.util.ArrayList;
import java.util.Optional;
import java.util.logging.Logger;

class InputAlphanumeric {

	private String strParam;
	float sumDigits = 0;
    
	/* Declare the logger object for the class to display the log messages */
	static Logger logger = Logger.getLogger(InputAlphanumeric.class.getName());

	public boolean validateInputs(String alphaNumericValue) {

		/* Fetch the class object's name from which this method is called */
		String cName = Thread.currentThread().getStackTrace()[2].getClassName();
		String mName = "validateInputs()";
		logger.info("This method '" + mName + "' called from the Class '" + cName + "'");

		/* Validating the function parameters using Optional String data type methods */
		Optional<String> strInput = Optional.ofNullable(alphaNumericValue);

		/*
		 * Fetch the value of the Optional String parameters using 'isPresent' condition
		 * check. If the value is available use 'get' method otherwise simply return
		 * 'false' to stop the program execution without proceeding further.
		 */
		if (strInput.isPresent()) {
			setStrParam(strInput.get());
		} else {
			return false;
		}

		logger.info("The provided Input validated Successfully");
		return true;
	}

	/* Define getter and setters for strParam & sumDigits variables */
	public String getStrParam() {
		return strParam;
	}

	public void setStrParam(String strings) {
		this.strParam = strings;
	}
	
	public float getSumDigits() {
		return sumDigits;
	}

	public void setSumDigits(float sumDigits) {
		this.sumDigits = sumDigits;
	}
}

class SumDgtsInAlphanumeric_Using_String extends InputAlphanumeric implements AlphanumericIntf {

	public float ParseAlphanumeric(String alphaNumericStr) {

		/* Fetch the class object's name from which this method is called */
		String cName = Thread.currentThread().getStackTrace()[2].getClassName();
		String mName = "ParseAlphanumeric";
		logger.info("This method '" + mName + "' called from the Class '" + cName + "'");

		if (validateInputs(alphaNumericStr)) {

			System.out.println("The given input alphaNumeric value is = '" + alphaNumericStr + "'");
			/*
			 * Traversing through each character of the alphanumeric input, and check whether
			 * any character is digit. If digit found, sum of all digits.
			 */
		   int lenOfStr=alphaNumericStr.length();
	        while (lenOfStr > 0) {
	        	if (Character.isDigit(alphaNumericStr.charAt(lenOfStr-1)))
	        	setSumDigits(getSumDigits()+Character.getNumericValue(alphaNumericStr.charAt(lenOfStr-1)));
	            lenOfStr=lenOfStr-1;
	        }

		} else
			logger.info("The given Input Alphanumeric is null");
		
        AlphanumericIntf.displayMsg(alphaNumericStr,getSumDigits());
		return getSumDigits();
	}
}

class SumDgtsInAlphanumeric_Using_Lamdba extends InputAlphanumeric implements AlphanumericIntf {

	public float ParseAlphanumeric(String alphaNumericStr) {

		/* Fetch the class object's name from which this method is called */
		String cName = Thread.currentThread().getStackTrace()[2].getClassName();
		String mName = "ParseAlphanumeric";
		logger.info("This method '" + mName + "' called from the Class '" + cName + "'");
		
		/* Define the Lambda expression for the user defined functional interface */
		/*
		 * Traversing through each character of the alphanumeric input, and check whether
		 * any character is digit. If digit found, sum of all digits.
		 */
		AlphanumericIntf ds = ((alphaNumStr) -> {
            int lenOfStr = alphaNumStr.length(), sum = 0;
            while (--lenOfStr >= 0)
                if (Character.isDigit(alphaNumStr.charAt(lenOfStr)))
                    sum += Character.getNumericValue(alphaNumStr.charAt(lenOfStr));
            return sum;
        });

		if (validateInputs(alphaNumericStr)) {

			System.out.println("The given input sentence is = '" + alphaNumericStr + "'");
			setSumDigits(ds.ParseAlphanumeric(alphaNumericStr));
			
		} else
			logger.info("The given Input Alphanumeric is null");
		
	    AlphanumericIntf.displayMsg(alphaNumericStr,getSumDigits());
		return getSumDigits();
	}
}

class SumDgtsInAlphanumeric_Using_Stream extends InputAlphanumeric implements AlphanumericIntf {

	public float ParseAlphanumeric(String alphaNumericStr) {

		/* Fetch the class object's name from which this method is called */
		String cName = Thread.currentThread().getStackTrace()[2].getClassName();
		String mName = "ParseAlphanumeric";
		logger.info("This method '" + mName + "' called from the Class '" + cName + "'");

		if (validateInputs(alphaNumericStr)) {

			System.out.println("The given Alphanumeric is = '" + alphaNumericStr + "'");
			ArrayList<Character> arrChars = new ArrayList<Character>();
		        for (char ch : alphaNumericStr.toCharArray())
		            arrChars.add(ch);
		        System.out.println("The list of Characters are= " + arrChars);
		 
		        /* Apply filter, map methods on the extracted List of characters. 
		         * If the digits found, accumulate sum of digits in the reduce method 
		         */
		        setSumDigits(arrChars.stream().filter(c -> Character.isDigit(c))
		        								.map(x -> Character.getNumericValue(x))
		        								  .reduce(0, (a, b) -> a + b));
		} else
			logger.info("The given Input Alphanumeric is null");

		AlphanumericIntf.displayMsg(alphaNumericStr,getSumDigits());
		return getSumDigits();
	}
}


public class SumofDgtsInAlphanumericJ8 {
	
	public static void main(String[] args) {

		/*
		 * create the class object SumDgtsInAlphanumeric_Using_String, and call the method
		 * ParseAlphanumeric
		 */
		SumDgtsInAlphanumeric_Using_String SDUStr = new SumDgtsInAlphanumeric_Using_String();
		SDUStr.ParseAlphanumeric("Java25Python35Yaers");

		/*
		 * create the class object SumDgtsInAlphanumeric_Using_Lamdba, and call the method
		 * ParseAlphanumeric
		 */
		SumDgtsInAlphanumeric_Using_Lamdba SDULambda = new SumDgtsInAlphanumeric_Using_Lamdba();
		SDULambda.ParseAlphanumeric("Tomcat35Jboss20Weblogic15Years");

		/*
		 * create the class object SumDgtsInAlphanumeric_Using_Stream, and call the method
		 * ParseAlphanumeric
		 */
		SumDgtsInAlphanumeric_Using_Stream SDUStream = new SumDgtsInAlphanumeric_Using_Stream();
		SDUStream.ParseAlphanumeric("");
		
		SDUStream.ParseAlphanumeric(null);
	}
}
OUTPUT:
Jan 27, 2023 3:30:31 PM pack1.SumDgtsInAlphanumeric_Using_String ParseAlphanumeric
INFO: This method 'ParseAlphanumeric' called from the Class 'pack1.SumofDgtsInAlphanumericJ8'
Jan 27, 2023 3:30:31 PM pack1.InputAlphanumeric validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.SumDgtsInAlphanumeric_Using_String'
Jan 27, 2023 3:30:31 PM pack1.InputAlphanumeric validateInputs
INFO: The provided Input validated Successfully
The given input alphaNumeric value is = 'Java25Python35Yaers'
The sum of digits in the given string 'Java25Python35Yaers' is 15.0


Jan 27, 2023 3:30:31 PM pack1.SumDgtsInAlphanumeric_Using_Lamdba ParseAlphanumeric
INFO: This method 'ParseAlphanumeric' called from the Class 'pack1.SumofDgtsInAlphanumericJ8'
Jan 27, 2023 3:30:31 PM pack1.InputAlphanumeric validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.SumDgtsInAlphanumeric_Using_Lamdba'
Jan 27, 2023 3:30:31 PM pack1.InputAlphanumeric validateInputs
INFO: The provided Input validated Successfully
The given input sentence is = 'Tomcat35Jboss20Weblogic15Years'
The sum of digits in the given string 'Tomcat35Jboss20Weblogic15Years' is 16.0


Jan 27, 2023 3:30:31 PM pack1.SumDgtsInAlphanumeric_Using_Stream ParseAlphanumeric
INFO: This method 'ParseAlphanumeric' called from the Class 'pack1.SumofDgtsInAlphanumericJ8'
Jan 27, 2023 3:30:31 PM pack1.InputAlphanumeric validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.SumDgtsInAlphanumeric_Using_Stream'
Jan 27, 2023 3:30:31 PM pack1.InputAlphanumeric validateInputs
INFO: The provided Input validated Successfully
The given Alphanumeric is = ''
The list of Characters are= []
The sum of digits in the given string '' is 0.0


Jan 27, 2023 3:30:31 PM pack1.SumDgtsInAlphanumeric_Using_Stream ParseAlphanumeric
INFO: This method 'ParseAlphanumeric' called from the Class 'pack1.SumofDgtsInAlphanumericJ8'
Jan 27, 2023 3:30:31 PM pack1.InputAlphanumeric validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.SumDgtsInAlphanumeric_Using_Stream'
Jan 27, 2023 3:30:31 PM pack1.SumDgtsInAlphanumeric_Using_Stream ParseAlphanumeric
INFO: The given Input Alphanumeric is null
The sum of digits in the given string 'null' is 0.0

The link to the JUnit5 test for this solution is here – https://atomic-temporary-185308886.wpcomstaging.com/2023/01/28/junit-test-sum-of-digits-in-an-alphanumeric/