0

I am trying to print out my methods calSum and calMean. I want to get a similar output to:

java RandomArray 8

0 9 5 3 5 6 0 8

Sum: 36

Mean: 4.5

but instead I am getting - Usage: java RandomArray . Example: java RandomArray 5

Am I doing something wrong in the printArray method? Or is it something else? Any help with the errors in my code would be great.

    public class RandomArray {

private int[] numbers; //instance variable

/**
 *  Constructor
 *
 *The size of the array
 */
public RandomArray(int size){
    numbers = new int[size];
    for(int i=0; i<numbers.length;i++){
        numbers[i] = (int)(Math.random()*10); // a random number between 0-9
    }
}

/**
 *  a method to print the array elements
 */

public void printArray() {
    for (int i = 0; i < numbers.length; i++)
        System.out.print("Java Random array:"+ numbers);
        System.out.println("Sum:" + calSum());
        System.out.println("Mean:" + calMean());
}       

/**
 *  A method to calculate the sum of all elements
 *
 */
public int calSum(){
 int sum = 0;
 for (int value : numbers) {
     sum += value;
}
    return sum;

}

/**
 *  A method to calculate the mean of all elements
 *
 *@return    The mean
 */

public double calMean() {
    int sum = calSum();
    int length = numbers.length;

    return (double) sum / length;
}


/**
 *  a method to print the array elements in reverse order
 */
public void printReverse(){


}

/**
 *  A main method to test
 */
public static void main(String[] args) {
    // Check to see if the user has actually sent a paramter to the method
    if (args.length != 1){
        System.out.println("Usage: java RandomArray <NUM>. Example: java RandomArray 5");
        System.exit(-1);
    }

    // Create an instance of the class 
    RandomArray test = new RandomArray(Integer.parseInt(args[0]));


    // Print the array
    test.printArray();

    // Calculate the sum of all the values in the array and print it
    System.out.println("Sum: "+ test.calSum());

    // Calculate the mean of all the values in the array and print it
    System.out.println("Mean: "+ test.calMean());

    System.out.print("Reverse: ");
    test.printReverse();
}

 }
  • 1
    you print the whole array instead of an element: use `numbers[i]`, i.e. `System.out.print("Java Random array:"+ numbers[i]);` – UninformedUser Dec 06 '18 at 13:36
  • 2
    Possible duplicate of [What's the simplest way to print a Java array?](https://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array) – Koenigsberg Dec 06 '18 at 13:38

1 Answers1

1

When you run the main class, the mainmethod accepts an array of elements of type String.

You have to keep in mind the call looks like java RandomArray arg1 arg2, even when you run it within an IDE. The array include all elements after java, even RandomArray.

So argswill always be made of at least 1 element. If you want the value arg1, you need to get args[1] not args[0].

Your code should looks like:

public static void main(String[] args) {
    // Check to see if the user has actually sent a paramter to the method
    if (args.length != 2){
        System.out.println("Usage: java RandomArray <NUM>. Example: java RandomArray 5");
        System.exit(-1);
    }

    // Create an instance of the class 
    RandomArray test = new RandomArray(Integer.parseInt(args[1]));

...

Next, you won't get the expected result when printing the generate array. Check the link in the comments to see how to properly print an array.

jhamon
  • 3,603
  • 4
  • 26
  • 37