fredy21
fredy21

Reputation: 41

Getting the size on an array from one method into another method where I will be filling the array with numbers

I must ask to the user for the size of the array. This method must be an integer. Then I must pass that value into another method where the user will fill the array, this method must be float.

It's throwing an error in this line float[] arraySize = new float [getLengthOfArray()]; In the second method.

The error says "Int[] cannot be converted to int". I don't know what to do.

Here's the entire code

    package Exercise;

import javax.swing.JOptionPane;

public class Exercise3 {

    public static int[] arrayLenght(){
        int arraySize = Integer.parseInt(JOptionPane.showInputDialog("Enter the array size"));
        int[] totalArraySize = new int[arraySize];
        return totalArraySize;
    }

    public static float[] fillArray(){
        float[] arraySize = new float [arrayLenght()];

        for (int i = 0; i < arraySize.length; i++) {
            arraySize[i] = Float.parseFloat(JOptionPane.showInputDialog("Enter a number"));
        }
        return arraySize;
    }

    public static void calculateArithmeticAverage(float[] numbers){
        int total = 0;
        int average = 0;

        for (int i = 0; i < numbers.length; i++) {
            total += numbers[i];
        }
        average = total / numbers.length; 
    }

    public static void main(String[] args) {
        calculateArithmeticAverage(fillArray());
    }

Upvotes: 0

Views: 36

Answers (1)

Eran
Eran

Reputation: 393771

Your arrayLength method should return an int, not an array.

public static int arrayLength()
{
    int arraySize = Integer.parseInt(JOptionPane.showInputDialog("Enter the array size"));
    return arraySize ;
}

Upvotes: 5

Related Questions