Question

I am new to java. I have an assignment that requires the following: Develop a program which allows the user to enter numbers into an array. Input will be as follows:

The user will enter the total number of integers to be entered into the array.

The user will then enter that number of unique integers (negative or positive). Do not allow the number of values entered to exceed the array size.

Develop methods to:

Sort the array

Determine the highest value

Determine the lowest value

Calculate the average value (double)

The code below does run but it displays the lowest number I enter as 0, which is not correct and the average does display in a double format but it always rounds the number (3.0 instead of 3.3). Can someone please let me know what I am doing wrong and how can I fix this. I have been googling the problem for 3 days now and can't find out where I went wrong.

import java.util.Scanner;
import java.util.Arrays;

public class UserArray2 {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        //allow user  input;
        System.out.println("How many numbers do you want to enter?");
        int num = input.nextInt();

        int numbers[] = new int[num];        
        int low = numbers[0];
        int max = numbers[0];
        int sum = 0;
        double avgNum;

        System.out.println("Enter the " + num + " numbers now.");

        for (int i = 0 ; i < numbers.length; i++ ) {
           numbers[i] = input.nextInt();
        }


        //sort
        System.out.println("The numbers you entered have been sorted from lowest to greatest:");
        Arrays.sort( numbers );
        for ( int i = 0 ; i < numbers.length ; i++ ) {

            System.out.println(numbers[i]);
        }

        //max

        for (int counter = 1; counter < numbers.length; counter++)
        {
         if (numbers[counter] > max)
         {
          max = numbers[counter];
         } 

         }

        //lowest

        for (int counter = 1; counter < numbers.length; counter++)
        {
         if (numbers[counter] < low)
         {
          low = numbers[counter];
         } 

         }

        //average
        for (int i : numbers)
        sum += i;
        avgNum = sum/numbers.length;


        System.out.println("The largest number on your list is: " + max);
        System.out.println("The lowest number on your list is: " + low);
        System.out.println("The average of the list of numbers is: " + avgNum);
    }


    public static void printArray(int arr[]){

        int n = arr.length;

        for (int i = 0; i < n; i++) {
            System.out.print(arr[i] + " ");
        }
    }

}
Was it helpful?

Solution

You are very close. Here are the problems that prevent your code from completing correctly:

  • You initialize min and max too soon. For min this creates problems when all numbers are positive; for max, when all numbers are negative. You should declare/initialize both variables after the reading has been completed. Moving the declaration to the point after the reading loop will fix this problem.

  • Although you correctly defined the type of avgNum as double, the expression that you assign it remains an int, because both its elements are int. Therefore, the division is truncated. Change the division as follows:

    avgNum = (double)sum/numbers.length;
    

or better yet, declare sum as double, and remove the cast.

OTHER TIPS

You have done these assignments prior taking the user inputs:

int low = numbers[0];
int max = numbers[0];

which means low and max initial value will be 0. So for your minimum logic, user inputs are compared with low and no value is lower than 0 so you always get 0 as the minimum.

You should define or re-assing them after taking the user input i.e with the real numbers array values.

In this line,

    avgNum = sum/numbers.length;

both sum and numbers.length are integers, so Java calculates the result as an integer. Then, it assigns the result to avgNum, which happens to be a double. By that time, any fractional part of the division result is already gone.

To fix this, you can:

    avgNum = (double)sum/numbers.length;

which casts sum to a double before performing the division.

  1. the array index starts from 0. but you are starting your counter from 1.

    for (int counter = 0; counter < numbers.length; counter++)

  2. you need to cast the int value to double. Else it will round to int.

    avgNum = (double)sum/numbers.length;

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top