• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

hamming code(array index out of bounds)

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i wrote a code to generate hamming code..i am getting the javaindexoutbounds exception..i even gave absurdly large array sizes to counter tht..still not working!

 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi sachin behl,

Welcome to CodeRanch!

Good to see that you are using code tags, but it would look even better if you follow Java coding style.

Apart from this, I assume that by javaindexoutbounds, you meant to say ArrayIndexOutOfBoundsException.

Well, this is because, you are accessing i and/or j location of array. Values of i and j depend on user input, however, size of array is fixed.

I hope this helps.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Yes, that really is poorly-formatted code
You are asking the user for the size of the array, but using 100 as a default size. Why?
Why have you got i<n+1 in your for loops?>
 
sachin behl
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey thank you for the replies !few things-
1)what do you mean by java coding style?!
2)i know that arrayindexoutofbounds occur when prog needs to access an index which is not in array(eg i=15 in a[10]
that is why i kept the array size 100(i made this change after i got the runtime error...oh and since i want to input the data from i=1 and not i=0, i have i<n+1...
3)even if i take the value of n as 3 and the size of array as 100,it still shows the runtime error...my question is which part of code is wong?!!
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The very first thing I noticed is that you're initializing the index variables i and j used to loop thru the arrays with a value of '1', but in Java an array index starts at 0.
Let's start there.
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sachin behl wrote:what do you mean by java coding style?!


By Java (not 'java') coding style, we mean this.

sachin behl wrote:i know that arrayindexoutofbounds occur when prog needs to access an index which is not in array(eg i=15 in a[10]
that is why i kept the array size 100(i made this change after i got the runtime error...oh and since i want to input the data from i=1 and not i=0, i have i<n+1...


Why are you not using 0th location of array? Any specific reason?

sachin behl wrote:even if i take the value of n as 3 and the size of array as 100,it still shows the runtime error...my question is which part of code is wong?!!


Debug, debug, debug You will get stack trace in case of exception. It will tell you at which line it is facing exception - that would make debugging easier.

I hope this helps.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you are going to have to write your path through that program on paper. You have lots of single-letter local variables, and poor code style with all the code crammed together, which makes it very difficult to read. Also you haven’t said where the exception occurs. You also haven’t explained the relation between n and the size of your arrays.
 
sachin behl
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I took the size of array like this afterwards..so as to make it large enough so as to prevent the error..
I ran the code on paper..k thanks anyways..the error occurs on line 28..
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have obviously got c beyond the permissible indices for the array h. You are incrementing c and doubling k; I think you will have to go through the execution (preferably with some print statements) and follow the values of k and c.

Where did you get the algorithm from? Are you sure you have copied it correctly? One < swapped for <= or similar and your indices will be out of bounds.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sachin behl wrote:I took the size of array like this afterwards..so as to make it large enough so as to prevent the error..



What exactly do you mean by that? If you want to store 4 ints in an array, then you need That will give you an array that can hold 4 items, at indices 0, 1, 2, 3, and arr.length will be 4.

If by "make it large enough to prevent the error" you mean that (using my 4-item array as an example), you find that you actually need to store 5 values instead of 4, then that's fine. But if you mean that you just tried a bigger number to see if it would make the error go away, then that is absolutely the wrong approach. You need to understand how arrays work, and what the indices and length will be for an array that meets your particular requirements.
 
sachin behl
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey all!!thank you very much...i got the answer...
the line 27 was a mistake...because i am checking for c<c+k while incrementing the value of c at the same time!!!this will keep on going!!
i tweaked this line according to the code required and i got the answer!!!it sure feels great to not see that error again!!

thank you people!!
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done sorting it out
 
reply
    Bookmark Topic Watch Topic
  • New Topic