Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-26-2007, 08:53 PM
Member
 
Join Date: Nov 2007
Posts: 2
j0shizabeast is on a distinguished road
Adding numbers in a 2 dimensional array
Hey guys I'm new to java and I am looking for some help. I want to create a program that will add up the numbers in a two dimensional array. I have most of the code down but I keep getting errors.

Here's my code:

class averageNum {
// initialize # of rows
int[][] arr = { { 1, 0, 12, -1 },
{ 7, -3, 2, 5 },
{ -5, -2, 2, 9 }
};


int x;
int sum;

public int sum(int num)
{
int total = 0;
}

public int test(int sum){

for (int i = 0; i < 3; i++)

for (int j = 0; j < 4; i++)
sum = sum + arr[i][j];

}



public static void main (String[] args){

System.out.println(sum);
}

I get an error in my main method saying "non static variable sum cannot be referenced from a static context"

Any help would greatly be appreciated!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-26-2007, 10:19 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 112
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
You need to instantiate an instance of your averageNum class. (BTW it is a good idea to being all of your class names with a capitol letter.) Before you do that, your averageNum needs some touching up.

Code:
public class AverageNum{ //initialize # of rows int[][] arr = { { 1, 0, 12, -1 }, { 7, -3, 2, 5 }, { -5, -2, 2, 9 } }; public AverageNum(){ //always need a constructor. } int x; //not sure why you need this int sum; //you may not need this either. you can just return the sum from the method //this method doesn't do anything public int sum(int num){ int total = 0; return total; } public int test(){ int sum = 0; for(int i = 0; i < arr.length; i++){ for(int j = 0; j < arr[i].length; j++){ sum = sum + arr[i][j]; } } return sum; } public static void main(String[] args){ AverageNum foo = new AverageNum(); System.out.println(foo.test()); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-27-2007, 05:31 AM
Member
 
Join Date: Nov 2007
Posts: 2
j0shizabeast is on a distinguished road
ok I understand now. Thank you very much appreciate the help!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to initialize a two dimensional Array Java Tip java.lang 0 04-14-2008 09:48 PM
Adding numbers in array Shaolin New To Java 1 11-15-2007 07:30 PM
generating random numbers in a 5x5 array. acidacid New To Java 3 08-14-2007 04:44 AM
Adding graphics to array romina Java 2D 1 08-01-2007 02:45 AM
Help with array multi-dimensional barney New To Java 1 07-31-2007 09:00 PM


All times are GMT +3. The time now is 09:20 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org