1

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));
 }
}
asked Dec 22, 2013 at 5:26
3
  • it depends how you want your decimal format to be looking like ? Commented Dec 22, 2013 at 5:28
  • Show you're desired output, current output, and input Commented Dec 22, 2013 at 5:30
  • (what i want) Kinda like this: 1,234.56 (rounds off after 2nd decimal number). (what i get) Error! Commented Dec 22, 2013 at 5:31

3 Answers 3

1

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));
}
answered Dec 22, 2013 at 5:40
Sign up to request clarification or add additional context in comments.

1 Comment

I tried your code, i got a compilation error? Also, the user is supposed to input the numbers...
1

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));
 ^^
answered Dec 22, 2013 at 5:41

14 Comments

Where does that go exactly? I am not sure which public static that i should replace?
This may be a ridiculous question, but may you do it for me? I don't understand it very well... :(
I still get errors? May you put that on my code because i believe i am placing it in the wrong place... :(
Take a look at my Edit 2. Shows all the fixes. Assuming all calculations are correct, I think the program should run now. I haven't tried to run it myself
Oh thanks! It works so far but the problem is that it doesn't go into decimal format?
|
0

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);
 }
answered Dec 22, 2013 at 13:17

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.