Question from the Java and Craftsmanship test

A Java function that adds numbers separated by commas or line breaks contained in a string.

Hard

This code

public final class StringCalculator {

  private static final String SEPARATOR = ",|\n";

  private StringCalculator() {}

  public static String sum(String numbers) {
    if (noNumbers(numbers)) {
      return "0";
    }

    return Arrays.stream(numbers.split(SEPARATOR))
        .map(BigDecimal::new)
        .reduce(BigDecimal.ZERO, BigDecimal::add)
        .toPlainString();
  }

  private static boolean noNumbers(String numbers) {
    return numbers == null || numbers.isBlank();
  }
}
Author: Clément DevosStatus: PublishedQuestion passed 111 times
Edit
0
Community EvaluationsNo one has reviewed this question yet, be the first!