CriCri
CriCri

Reputation: 189

Return one value from an array when calling a method

when I was writing my program I came across a problem with arrays and can't find any explanations on how to fix it.

The problem i ran into is how i should go about using an array from the main a method and passing it to a method, but only getting one value as a return rather than the whole array again.

note: the way i have my program set up the array is filled with random numbers every time it is ran.

im not too sure if you can do this or not, but

The way i am currently calling a method is by doing as follows:

 "declared variable (not array)" = "name of method" ("the actual array name")
     totalSum = sumTotal (randomArray) // example

The method I'm trying to call:

 public static int[] sumTotal (int[] totals) {
 int total = 0;
 for (int i = 0; i < totals.lenght; i++) {
   total += totals[i];
   }
   return total;
  }

it keeps giving me a "cannot find symbol

symbol  : method sumTotals(int[])
location: class oneDimensionArraysNew"

error when i try to compile,

im not too sure how i would go about fixing this, I appreciate any and all help!!

Upvotes: 1

Views: 494

Answers (2)

libik
libik

Reputation: 23049

Here you say, the return value is array of int : public static int[] sumTotal...

Just change it to : public static int sumTotal...

Upvotes: 4

Joshua Hyatt
Joshua Hyatt

Reputation: 1340

public static int[] sumTotal(int[] totals {
...
}

The return type should be int, not int[].

public static int sumTotal(int[] totals {
...
}

Upvotes: 2

Related Questions