Java List Sum sumCreditList(List list)

Here you can find the source of sumCreditList(List list)

Description

Creates the sum of a list of credit ranges, returning the cumulative min and max credits eg "1-2", "3-5" would return "4-7"

License

Open Source License

Declaration


public static String sumCreditList(List<String> list) 

Method Source Code

//package com.java2s;

import java.util.*;

public class Main {
    /**/*from w  ww  . j a  v  a2s.c  o  m*/
     * Creates the sum of a list of credit ranges, returning the cumulative min and max credits
     * eg "1-2", "3-5" would return "4-7"
     */

    public static String sumCreditList(List<String> list) {
        if (list == null || list.isEmpty())
            return "0";
        float minCredits = 0;
        float maxCredits = 0;
        for (String item : list) {
            String[] split = item.split("[ ,/-]");

            String first = split[0];
            float min = Float.parseFloat(first);
            minCredits += min;

            String last = split[split.length - 1];
            float max = Float.parseFloat(last);
            maxCredits += max;
        }

        String credits = Float.toString(minCredits);

        if (minCredits != maxCredits) {
            credits = credits + "-" + Float.toString(maxCredits);
        }

        credits = credits.replace(".0", "");
        return credits;
    }
}

Related

  1. sum(List numbers)
  2. sum(List listOfNumbers)
  3. sum_ints(List list)
  4. sumAll(List list)
  5. sumAllColumnsOfMatrix(List> matrix)
  6. sumDifferences(List a, List b)
  7. sumDouble(List list)
  8. sumInteger(List list)
  9. sumIntegers(List values)