• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

ArrayIndexOutOfBoundsException in 2D Matrix.

 
Ranch Hand
Posts: 94
1
Oracle Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 8507
610
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 14496
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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].
 
Marshal
Posts: 5466
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 76830
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Without deviation from the norm, progress is not possible - Zappa. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic