Results 1 to 4 of 4
Thread: exception
- 08-16-2009, 11:45 AM #1
Member
- Join Date
- Aug 2009
- Posts
- 1
- Rep Power
- 0
exception
Java Code:/*design3.java*/ class Design3 { public static void main(String args[]) { char arr[][]=new char[5][]; arr[0]=new char[5]; arr[1]=new char[4]; arr[2]=new char[3]; arr[3]=new char[2]; arr[4]=new char[1]; int i,j; for(i=0;i<5;i++) for(j=4;j>i+1;j--) arr[i][j]='+'; for(i=0;i<5;i++) { for(j=5;j>i+1;j--) System.out.print(arr[i][j]+" "); System.out.println(); } } }
output
C:\src>java Design3
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at Design3.main(Design3.java:16)
help me guys.
-
1) Walk through your code on paper as if you yourself were the computer and see what the array indices are as you loop through your program, then you'll immediately see your error.
2) An unrelated problem, but one that will effect your output in the second loop (after you fix its array index out of range problem: Always enclose any for loop, while loop, if block, etc... in curly braces. If you don't, you will shoot yourself in the foot (like this program will do).
Best of luck.
- 08-16-2009, 12:44 PM #3
Member
- Join Date
- Aug 2009
- Posts
- 49
- Rep Power
- 0
Hi Mohneesh,
Put the for loops in try block followed by catch statement for (ArryIndexOutOfBounds e)
{
System.out.println(e.geMessage));
}
write to me if still problem persists
cordially,
Anand Ravindran
Webarch Technologies
:: Java Needs - Java Programmers' Website ::
-
Hm, I politely disagree with using a try / catch block here as this is an exception that shouldn't ever happen. It's like using try / catch for a NPE.
Again if you think through the logic, you should find the solution. What is the ultimate goal of this program, by the way?
Similar Threads
-
Need help on Exception
By Deon in forum New To JavaReplies: 7Last Post: 02-11-2010, 05:46 PM -
why this exception
By payal.mitra86 in forum JDBCReplies: 1Last Post: 05-21-2008, 10:28 PM -
Exception!
By rameshraj in forum Advanced JavaReplies: 1Last Post: 05-05-2008, 01:39 PM -
Trouble with factory method - unhandled exception type Exception
By desmond5 in forum New To JavaReplies: 1Last Post: 03-08-2008, 06:41 PM -
Exception
By Camden in forum New To JavaReplies: 2Last Post: 11-26-2007, 11:50 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks