This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes please can you help to identify those errors Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "please can you help to identify those errors" Watch "please can you help to identify those errors" New topic
Author

please can you help to identify those errors

Nicholas Dintshi
Greenhorn

Joined: Apr 04, 2008
Posts: 3
hi,from this code i do encounter some erros

class Array06
{
public static void main(String [] args)
{
int[][] arrayOfArray = new int[][];

int[] array1 = {10,20,30,40,50};
int[] array2 = {100,200,300};
int[] array3 = {5,10,15,20};

arrayOfArray[0] = array1;
arrayOfArray[1] = array2;
arrayOfArray[2] = array3;

int total = 0,noOfElements=0;

for(int i = 0;i<arrayOfArray.length;i++)
{
for(int j = 0;j<arrayOfArray[i].length;i++)
{
total += arrayOfArray[i][j];
noOfElements++
}
}

System.out.println("Total is:" + total);
System.out.println("Average:" + total/noOfElements);
}
}
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9946
    
    6

What are the errors? does it compile? does it run?


Never ascribe to malice that which can be adequately explained by stupidity.
shilpa deshpande
Greenhorn

Joined: Sep 06, 2007
Posts: 7
There are two compile time errors.

1) Java needs all the dimensions of the array to be definite other than the last dimension. For example here, int[][] arrayOfArray = new int[3][];
will work beautifully.

2) There is a semicolon missing on line # 22.

One runtime error happens because in the loop on line # 19, instead of counter j, counter i is incremented which results in throwing the ArrayIndexOutOfBoundsException.

Tip: never copy the for loops, it takes hours to debug such small things.

The updated code looks like this.


Hope it helps
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

Nicholas Dintshi and Shilpa Deshpande,
Welcome to JavaRanch.

Nicholas please use code tags and other UBB tags as required. They make the post much more readable.

Hope you both have good time here. Good Luck..!!
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: please can you help to identify those errors
 
Similar Threads
Question on similar & identical of Objects
convert ArrayList of String[] to String[][]
Comparing arrays
How to do �shallow copy� and �deep copy�?
Returning an array