Is there a function in Jave to calculate the mean ?
I’ve tried by myself but I’m afraid that sum has too high value: 5 642 782 840
Can a integer be 5 642 782 840 in Java ? I don’t think so…
for (int i = 0; i < timeValues.length; i++) {
sum = sum + Integer.parseInt(timeValues[i]);
}
time = Integer.toString(sum / timeValues.length);
thanks
Integercan hold values between -2.147.483.648 and 2.147.483.647.Longcan hold values between -9.223.372.036.854.775.808 and 9.223.372.036.854.775.807.Thus a
longwould be suitable for your use case.Another solution to do it without libraries would be to use
java.math.BigIntegerfor your calculations. It supports even bigger numbers. At least if you don’t use floating point values likefloatordouble. Of what type are your values in the timeValues array?If you want to use a library you may want to have a look at Apache Commons Math. To be more precise: have a look at DescriptiveStatistics and SummaryStatistics.