Wodes
Wodes

Reputation: 1

How to calculate average from command line? Homework excercize

I have to make a program which calculates the average, the modal value and the median of some numbers insert on command line. The numbers have to be in a range [1,10] I can't understand why it stops. Where am I wrong? This is the code:

import java.lang.Integer;
import java.util.Arrays; 

class Stat{
    public static void main(String[] args){


        int i,median,modalValue = 0;
        float average, sum = 0;
        int repetition[] = new int[args.length];
        Integer allNumbers[] = new Integer[args.length];


        //check numbers range
            try{

                //reading args from command line
                for( i = 0; i < args.length; i++){
                    //if not in range --> exception
                    if(Integer.parseInt(args[i]) < 1 || Integer.parseInt(args[i]) > 10)
                        throw new Exception("Exception: insert number out of range. Restart the programm.");

                //put numbers in an array
                allNumbers[i] = new Integer(args[i]);
                }

                //sorting the array
                Arrays.sort(allNumbers);

                //calculate average
                for( i = 0; i < allNumbers length; i++ ){
                    sum += allNumbers[i];
                }
                average = sum / (i + 1) ;
                System.out.println("Average: " + average);

                //calcolate modal value (most frequent number)
                for( i = 0; i < repetition.length; i++){        //counting numbers occurrences
                    repetition[allNumbers[i]]++;
                }

                for( i = 1; i < repetition.length; i++){        //checking which number occurrences the most
                    if(repetition[i] >= repetition[i-1])
                        modalValue = repetition[i];
                }

                System.out.println("Modal Value: " + modalValue);


                //calculating median value
                if((allNumbers.length) % 2 == 0){       //even
                    median = allNumbers.length/2;,
                }else{                                  //odd
                    median = (allNumbers.length/2) + 1; 
                }
                System.out.println("Median: " + allNumbers[median]);


            }catch(Exception e) {                   //out of range
                System.out.println(e.getMessage());
            }




    }

}

Upvotes: 0

Views: 100

Answers (1)

Joe Daley
Joe Daley

Reputation: 46456

First Step

You will get more information about errors if you remove that try/catch. By catching the exception and printing out just its message, you are missing out on its stack trace, which will tell you exactly which line the error occurs on.

So firstly, remove the try/catch, and allow your main method to throw exceptions by changing its declaration to: public static void main(String[] args) throws Exception {. Compile and run it again, to allow the exception to crash the program.

What type of exception is thrown? What line number does it say?

Second Step

Next, let's look at some of your code that helps calculate the modal value:

int repetition[] = new int[args.length];

for (i = 0; i < repetition.length; i++) {
    repetition[allNumbers[i]]++;
}

The first line will create an array with the same number of elements as numbers provided on the command line. For example, if you provide numbers 5 8 9, that's three numbers, so the array will have three elements. Array indexes start at zero, so the indexes will be 0, 1 and 2.

The for loop will then take the first number, allNumbers[0] which in my example is a 5, and increment the value in the array at index 5. This causes an exception because the array does not have an index 5. That would be "outside the bounds" of the array.

The problem here is how you create the repetition array. Creating it with only three elements is not enough. You need to think about how to create it so that it will be able to handle any number in the range of [1, 10] that you were given.

Upvotes: 1

Related Questions