Sum of digits in string

The problem

Write a program that asks the user to enter a series of single digit numbers with nothing separating them. The program should display the sum of all the single digit numbers in the string. For example, if the user enters 2514, the method should return 12, which is the sum of 2, 5, 1, 4. The program should also display the highest digit in a string and lowest digits in string.

Collecting user input from the keyboard we will separate the string on each digit using a regular expression then create a java 8 stream. At this point the stream contains elements of type string so we will need to convert to doubles by calling mapToDouble function. Finally calling summaryStatistics will create an object DoubleSummaryStatistics to allow calling multiple reduction operations on a stream such as max, min and sum. Attempting to create a stream and calling multiple operations will result in a stream closed exception.

Breaking it down

public static void main(String[] args) {

    // Create a Scanner object for keyboard input.
    Scanner keyboard = new Scanner(;

    // Get a string of digits.
    System.out.print("Enter a string of digits: ");
    String input = keyboard.nextLine();

    // close keyboard

    DoubleSummaryStatistics summaryStats = Stream.of(input.split(""))

    System.out.println("The sum of numbers " + summaryStats.getSum());
    System.out.println("The highest digit is " + summaryStats.getMax());
    System.out.println("The lowest digit is " + summaryStats.getMin());


Enter a string of digits: 12345
The sum of numbers 15.0
The highest digit is 5.0
The lowest digit is 1.0