1

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!!

2 Answers 2

4

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

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

Sign up to request clarification or add additional context in comments.

4 Comments

by doing so i wouldn't have to return the array? let me try
You return what you want to return. If you have int[] you return array of int. If you have String you return String, if you have int you return int... :)
I completely overlooked that
never mind i guess I'm overlooking things a bit, I really appreciate your help, would you mind +1 the thread so i can +1 you?
2
public static int[] sumTotal(int[] totals {
...
}

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.