Ask Experts Questions for FREE Help !
Ask
    lost java's Avatar
    lost java Posts: 1, Reputation: 1
    New Member
     
    #1

    Nov 23, 2015, 11:58 AM
    Method to calculate average in java not working properly
    I am trying to call in a method to calculate average from data that has been input in an array using the scanner class, but somehow the average formula is not working properly. I have tried correcting it for a long time but the output looks something like "[I@34d0cdd0" in place of numbers. Can you please help point out what am I doing wrong? I am new to this, any help is really appreciated. Thanks.
    import java.util.Scanner;
    public class Programxx {
    public static void main(String[] args) {
    //Declare variables
    int number;


    Scanner input = new Scanner (System.in);

    System.out.println("Enter the numbers you want to calculate the average of : ");
    //get total numbers needed for average
    number = input.nextInt();

    //validate input (for average there should at least be 2 numbers)
    while (number <=1)
    {
    System.out.print("You didn't enter a valid value. Enter the numbers you want to calculate the average of : ");
    number = input.nextInt();
    }

    //create array for numbers entered
    int[] ar = new int[number];

    for (int I = 0; I < number; I++) {
    System.out.print("Number " + I + " : ");
    ar[i] = input.nextInt();
    }
    input.close(); // input close

    average (ar);
    System.out.println("The total average for the array of numbers is : " + ar);
    }
    // method to calculate and return the average of passed array
    public static double average (int [] array) {

    double avgs;
    double total=0;

    for (int I = 0; I < array.length; I++)
    {
    total =array[i] + total;
    }
    avgs = (total/array.length);
    return avgs;
    }
    }
    CravenMorhead's Avatar
    CravenMorhead Posts: 4,532, Reputation: 1065
    Adult Sexuality Expert
     
    #2

    Nov 24, 2015, 09:04 AM
    When you call average(ar) you're discarding the return value and using the address for the array to print out.

    Try adding a new variable and capture the return value from the average method. Then output that variable instead of ar.

    Does that make sense?

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

How do I calculate gross profit using the weighted average method and the FIFO method [ 0 Answers ]

Please give me the fomular and where to look, which amounts

Weighted average method [ 1 Answers ]

Beginning work in process 10000 units Units started 20000 units Units completed 25000 units Beginning work in process direct materials $6000 Beginning work in process conversion $2600 Direct materials added during month $30000 Direct manufacturing labor during month $12000 Factory overhead...

FIFO cost method from the weighted-average cost method [ 4 Answers ]

On December 31, 2006 Company appropriately changed to the FIFO cost method from The weighted-average cost method for financial statement and income tax purposes. The change will result in a $70,000 increase in the beginning inventory @January 1, 2006. Assuming a 40 Percent income tax rate, the...

Weighted Average Method [ 1 Answers ]

You are employed by Spirit Company, a manufacturer of digital watches. The company’s chief financial officer is trying to verify the accuracy of the ending work in process and finished goods inventories prior to closing the books for the year. You have been asked to assist in this verification. The...

Java: using an array from the main in a method [ 2 Answers ]

I have the array names in the main of my program and I want to use it in a method FindName.. How would I wright the method header and the call to the method in the main?


View more questions Search