Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I wrote a snippet of code and i can't get it to be formatted (decimal).

Here is my code:
Collapse | Copy Code
Java
import java.text.DecimalFormat;
import java.util.*;
public class Mean {
    public static void main(String [] args){
        Scanner s = new Scanner(System.in);
            double[]x = new double[10];
            int i;
            for(i = 0;i < 10; i++){
                x[i] = s.nextDouble();
            }
            double mean = mean(x, i);
            double deviation = var(x);
            System.out.println("The mean is " + mean); 
            System.out.println("The standard deviation is " + deviation);
    }
public static double sum(double[] a) {
        double sum = 0.0;
        for (int i = 0; i < a.length; i++) {
            sum += a[i];
        }
        return sum;
    } 
    public static double mean(double[]x, double i){ 
        if (x.length == 0) return Double.NaN;
        double sum = sum(x);
        return sum / x.length;
    } 
    public static double var(double[] x) {
        if (x.length == 0) return Double.NaN;
        double avg = mean(x, 10);
        double sum = 0.0;
        for (int i = 0; i < x.length; i++) {
            sum += (x[i] - avg) * (x[i] - avg);
        }
        DecimalFormat myFormatter = new DecimalFormat("#,##0.00");
        return sum / myFormatter.format((Math.sqrt(x.length - 1)));
    }
}
Posted
Comments
Sergey Alexandrovich Kryukov 21-Dec-13 23:08pm    
Not a good question.

You need to do the following:

If the behavior of your code does not match what you expected, use the debugger and fix it. If you failed to do so, describe what did you want to achieve, what's expected behavior, what is observed behavior, why do you feel it's wrong. If you have some exceptions, describe steps to reproduce, complete exception information. Comment the points in code related to exception throwing/propagation. Post all what's relevant. See also: http://www.sscce.org.

—SA
Richard MacCutchan 22-Dec-13 3:51am    
The statement return sum / myFormatter.format((Math.sqrt(x.length - 1))); does not look right.

Your formula for standard deviation seems odd, you may want to check with google. However, that is not issue here.
You should only do the formatting on the return double value in the calling method not in the called method. See the changes below, I am assuming you want double with 2 decimal places, if not you can always ask google.

C#
public static void main(String [] args){
             
          // your other code
          double deviation = var(x);
          DecimalFormat df = new DecimalFormat("#.##");
          System.out.println("The standard deviation is " + df.format(deviation));
    }

    public static double var(double[] x) {
        // your other code
        double deviation = Math.sqrt(sum/(x.length - 1));
        return deviation;
        //DecimalFormat myFormatter = new DecimalFormat("#,##0.00");
        //return sum / myFormatter.format((Math.sqrt(x.length - 1)));
    }
 
Share this answer
 
v2
it's alright buddy, i already figured it out... ;D
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 29-Dec-13 19:55pm    
Posting such things as is considered as abuse. Do do it.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900