Wumbo
Wumbo

Reputation: 41

Decimal Format Issue Java

I wrote a program and I can't figure out how i should do the decimal format? I thought i was doing it right, apparently i can't do decimal format correctly. Can, someone help me with the decimal format?

Here is my code:

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));

    }
}

Upvotes: 1

Views: 554

Answers (3)

Aroon
Aroon

Reputation: 177

You need to format the double at the time of print.If you tried at var() function and then assign with double deviation then it will give numberformatException due to special character that returns by Var().You can run this code It will give you the output.

 public static void main(String[] args) throws ParseException {
            Scanner s = new Scanner(System.in);
            double[]x = new double[10];
            int i;
            for(i = 0;i < 2; i++){
                x[i] = s.nextDouble();
            }
            double mean = mean(x, i);
            double deviation = var(x);
            DecimalFormat myFormatter = new DecimalFormat("#,##0.00");
            System.out.println("The mean is " + mean);
            System.out.println("The standard deviation is " +myFormatter.format(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);
            }
            return sum / Math.sqrt(x.length - 1);
        }

Upvotes: 0

Paul Samsotha
Paul Samsotha

Reputation: 209132

The problem is the formatter return a String. you should return a String in the metod signature

public static String var(double[] x) 

Also this line

return sum / myFormatter.format(Math.sqrt(x.length - 1));

Will not work if you are to return a String. You should do the calculations first, and then format it. Then return the formatted number

Edit: Try this, see if it works

public static String var(double[] x) {
    if (x.length == 0) 
        return null;

    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");
    double result = sum / Math.sqrt(x.length - 1);

    return myFormatter.format(result);

}

Then where you have this double deviation = var(x); replace it with String deviation = var(x);

Edit 2: complete code

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);
            String deviation = var(x);                  // changed to String
            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 String var(double[] x) {         // changed return
        if (x.length == 0) 
            return null;

        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");
        double result = sum / Math.sqrt(x.length - 1);

        return myFormatter.format(result);

    }
}

Update: correct standard deviation formula

double result = Math.sqrt(sum / (x.length - 1));

                          ^^

Upvotes: 1

Elliott Frisch
Elliott Frisch

Reputation: 201537

Try this to format your double(s)... I also had to update you method var -

public static String formatDouble(double in) {
  DecimalFormat myFormatter = new DecimalFormat(
      "#,##0.00");
  return myFormatter.format(in);
}

public static double var(double[] x) {
  if (x.length == 0)
    return Double.NaN;
  double avg = mean(x);
  double sum = 0.0;
  for (int i = 0; i < x.length; i++) {
    sum += (x[i] - avg) * (x[i] - avg);
  }
  return sum / Math.sqrt(x.length - 1);
}

public static void main(String[] args) {
  double[] x = new double[] {
    2000.20, 1000.10, 3000.30, 4000.40,5000.50,
    6000.60,7000,70,8000.80,9000.90
  };
  double mean = mean(x);
  double deviation = var(x);
  System.out.println("The mean is "
      + formatDouble(mean));
  System.out.println("The standard deviation is "
      + formatDouble(deviation));
}

Upvotes: 1

Related Questions