Results 1 to 4 of 4

Thread: exception

  1. #1
    mohneesh is offline Member
    Join Date
    Aug 2009
    Posts
    1
    Rep Power
    0

    Smile 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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    Join Date
    Aug 2009
    Posts
    49
    Rep Power
    0

    Default

    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 ::

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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

  1. Need help on Exception
    By Deon in forum New To Java
    Replies: 7
    Last Post: 02-11-2010, 06:46 PM
  2. why this exception
    By payal.mitra86 in forum JDBC
    Replies: 1
    Last Post: 05-21-2008, 11:28 PM
  3. Exception!
    By rameshraj in forum Advanced Java
    Replies: 1
    Last Post: 05-05-2008, 02:39 PM
  4. Replies: 1
    Last Post: 03-08-2008, 07:41 PM
  5. Exception
    By Camden in forum New To Java
    Replies: 2
    Last Post: 11-27-2007, 12:50 AM

Posting Permissions

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