0
import java.util.Scanner; 
import java.util.Arrays; 
public class Improved { 

    //I resize the array here so that it only counts inputs from the user
    //I want to ignore the 0 input from the user 
            //I think the error happens here or in my main method
    public static double[] resizeArray(double[] numbers, double size) {  
        double[] result = new double[(int)size];
        for (int i = 0; i < Math.min(numbers.length, size); ++i) {
            result[i] = numbers[i];
        }
        return result; 
    }

    //compute average nothing is wrong here
    public static double getAverage( double[] numbers) { 
        double sum = 0;
        for (int i = 0; i < numbers.length; ++i) 
            sum += numbers[i]; 
        double average = sum/numbers.length; 
        return average; 
    }

    //SD nothing is wrong here
    public static double getSD( double[] numbers, double average) { 
        double sd = 0; 
        for ( int i = 0; i < numbers.length; ++i) 
            sd += ((numbers[i] - average)*(numbers[i] - average)/ numbers.length); 
        double standDev = Math.sqrt(sd); 
        return standDev; 
    }

    //maximum nothing is wrong here
    public static double getMax( double[] numbers) { 
    double max = numbers[0]; 
    for (int i = 1; i < numbers.length; ++i)
        if (numbers[i] > max){
            max = numbers[i];
    }
    return max; 
    }

    //minimum nothing is wrong here
    public static double getMin( double[] numbers) { 
    double min = numbers[0]; 
    for (int i = 1; i < numbers.length; ++i)
        if (numbers[i] < min) {
            min = numbers[i];
        }
    return min; 
    }

    //median value nothing is wrong here
    public static double getmed( double[] numbers) { 
        double median;
        if (numbers.length % 2 == 0) 
            median = (((numbers[numbers.length/2 - 1]) 
                + (numbers[numbers.length/2]))/2);
        else
            median = numbers[numbers.length/2];

        return median; 
        }
//the problem is in the main method i think or in the call method to resize
public static void main(String[] args) { 

    Scanner input = new Scanner(System.in); 
    double[] statArr = new double[99]; 
    double size = 0;
    int i = 0;
    System.out.println("Type your numbers: ");
        double number = input.nextDouble(); 
        //I don't want the zero in the array, I want it to be excluded
        while (number != 0){
            statArr[i] = number;
            i++; 
            number = input.nextDouble();
            ++size;
        if ( size == statArr.length) { 
            statArr = resizeArray(statArr, statArr.length * 2); 
        }
        ++size;
    }
    statArr = resizeArray(statArr, size);

    java.util.Arrays.sort(statArr);

    double average = getAverage(statArr);

    System.out.println( "The average is " + getAverage(statArr));

    System.out.println( "The standard deviation is " +  getSD(statArr, average));

    System.out.println( "The maximum is "  + getMax(statArr));

    System.out.println( "The minimum is " + getMin(statArr));
    }
}

// I don't have any concerns with computing the math parts, but I can't seem to make it so my array ignores the 0 that ends the while loop. In other words, I want every number included up until the user enters the number 0. Everything else is right. Thank you very much!

  • I think you're having [this issue](http://stackoverflow.com/questions/8194917/comparing-a-double-against-zero). – m0skit0 Nov 06 '13 at 21:36
  • I doubt that. If the user inputs 0.0, then the condition number!=0 will be false. No doubt about that. – Giovanni Botta Nov 06 '13 at 21:37
  • You will probably be better off using an ArrayList and Collections.sort() instead of an array that you manually resize (that's what ArrayList does!). Also, you should run your code in a debugger to figure out why it doesn't do what you expect. – Giovanni Botta Nov 06 '13 at 21:38
  • @Giodude You're correct, sir. – m0skit0 Nov 06 '13 at 21:41

1 Answers1

2

You have ++size twice. This means your resizeArray method won't work correctly:

double[] result = new double[(int)size];

Here you're allocating more than what you actually want. This is why you're getting zeroes in your array. Java arrays are initialized to 0 (in case of numeric primitive types).

As Giodude already commented, I suggest you using List implementations (typically ArrayList) instead of arrays everytime you can.

Also size could be declared as int altogether and avoid that cast (and save some extremely slight memory), you're not using it as a double anywhere.

m0skit0
  • 25,268
  • 11
  • 79
  • 127
  • 2
    Definitely true. Nice catch. I stand by my comment of using an ArrayList which would've avoided this problem all together. – Giovanni Botta Nov 06 '13 at 21:44
  • @Giodude Definitely using a List is much better. Java 101: always use a List unless you're really can't! – m0skit0 Nov 06 '13 at 21:47