• Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayIndexOutOfBoundsException in 2D Matrix.  RSS feed

 
Rrohit rakesh upadhyay
Ranch Hand
Posts: 94
1
Java Notepad Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to fill a 2D matrix of dimension n*n, where n is odd like this:


and i wrote the following program for it



But it gives me ArrayIndexOutOfBoundsException in line 39. Can you please tell me how does the index goes out of bounds when the condition is clearly specified in the while loop.
 
Liutauras Vilda
Marshal
Posts: 4651
319
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

First of all try to clean the mess in your code by getting rid off variable names as l, sz, t, b, r, test, num. Give something meaningful, so everyone could understand, and especially yourself.

What this line of code does? Answer simply would be hell knows.

And please copy/paste exact error message you're getting. More specifically we are interested about exact line where the exception being thrown. Will be the way more clearer than "But it gives me index out of bounds exception for the first line in the third "else if's" for loop."

Such a problems easy to solve by adding print statements and tracking variable values. Example write System.out.println("Line 18, i = " + i);" and similar. And put them on every single line if it is needed and you'll see where your logic flaws.
 
Stephan van Hulst
Saloon Keeper
Posts: 7817
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use names such as height, width, row, column (although with grids I tend to resort to x and y). They make it much more clear what's going on.

Just a tip, I mostly find it easiest to access grids with rows first, then columns. Using grid[y][x] may seem a bit weird at first, but from experience it makes many problems (like printing) a lot easier than grid[x][y].
 
Tim Cooke
Marshal
Posts: 3873
233
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm afraid to say it, but that is probably one of the most unreadable and cryptic pieces of code I have seen in a long time, and I've seen a lot of terrible code in my time. How is anyone supposed to figure out what it's supposed to do, let alone how it does it?

Start by giving your variables meaningful names. Even this single line:
raises so many questions. What are the variables for? sz? b? l? r? dir? What do their initial values mean?

It's a mess. A good exercise at this point is to refactor your code to make it more readable and the details of what it's actually doing become clearer. Quite often I find by doing that process alone you are likely to stumble upon the error in your logic.
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also remove every single line of that code from the main method. Everything belongs in a different place. Your main method should read like thisAs well as the code being impossible to read, it is also impossible to use. How is the user supposed to know what to enter and when to enter it?
What have spirals got to do with it?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!