# Thread: sum of array

1. Member
Join Date
Mar 2012
Posts
7
Rep Power
0

## sum of array

Hello friends,
am trying to find the sum of thenumbers in the array i trie the following code but didnt get the answer

public static void main(String args[]){
int k[] = {2,5,6,7,12,34,6,7,8,9,20,21,2,2};
int t = sum(k,0);
System.out.println("Sum = "+t);

can you guys help me

2. ## Re: sum of array

What is the sum method? If you want help, you'll have to provide an SSCCE that demonstrates exactly what you're doing, along with any errors or exceptions you receive.

3. Member
Join Date
Mar 2012
Posts
7
Rep Power
0

## Re: sum of array

cannot find symbol method sum(int[],int), thats the error i getting

4. Member
Join Date
Mar 2012
Posts
7
Rep Power
0

## Re: sum of array

the code is

public class sum {
public static void main(String args[]){
int k[] = {2,5,6,7,12,34,6,7,8,9,20,21,2,2};
int t = sum(k,0);
System.out.println("Sum = "+t);
}
}

5. ## Re: sum of array

Makes sense, since sum() is not a standard method that you can just use whenever you want. Sounds like your assignment is to write the sum method yourself. Recommended reading: Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

6. Moderator
Join Date
Apr 2009
Posts
13,319
Rep Power
23

## Re: sum of array

If you haven't written a sum() method then Java won't create one for you.
You need to write the code to loop around that array adding the numbers up.

7. Member
Join Date
Mar 2012
Posts
7
Rep Power
0

## Re: sum of array

thank you,but my intention is to get the sum of all numbers in the array
can you give me the right code,or my code is wrong or not?

8. Member
Join Date
Mar 2012
Posts
7
Rep Power
0

thank you

9. ## Re: sum of array

We aren't just going to "give you the code" and asking us to do so is pretty rude. Make an attempt. Did you read the link I already gave you?

10. Member
Join Date
Mar 2012
Posts
7
Rep Power
0

## Re: sum of array

sorry thank you

11. ## Re: sum of array

Originally Posted by Tolls
If you haven't written a sum() method then Java won't create one for you.
It doesn't? darn, that's why the compiler keeps on whining about my 'makeMeFilthyRichNow()' method call.

kind regards,

Jos

12. ## Re: sum of array

Try looking in the tutorials I have written. I believe one shows how to add an array recursively. This should be easily adapted to do w/o recursion.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•