Meaningless Drivel is fun!
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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "please can you help to identify those errors" Watch "please can you help to identify those errors" New topic

please can you help to identify those errors

Nicholas Dintshi

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];

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

Joined: Oct 02, 2003
Posts: 11955

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

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
shilpa deshpande

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:
subject: please can you help to identify those errors
It's not a secret anymore!