• Post Reply Bookmark Topic Watch Topic
  • New Topic

Arrays  RSS feed

 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I do not believe that my for loop is working properly, if I enter any input, the program errors.
I think that the i is not incrementing my arrays, do you see anything that could be fixed?

 
Carey Brown
Saloon Keeper
Posts: 3309
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post the exact, complete, error message?
 
Siddarth Cherukumudi
Ranch Hand
Posts: 36
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The length of the arrays which you have declared is zero. because of that you cannot put any elements in the array and thats the reason why you are getting indexoutofbounds exeption

Regards,
Siddartha C.S
 
Carey Brown
Saloon Keeper
Posts: 3309
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This array was initialized to have a length of zero but you attempt to use index of 1 (remembering that indexes start with 0).

To be clear, you can't even use an index of 0 unless the array length is at least 1.
 
Carey Brown
Saloon Keeper
Posts: 3309
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have two loops, one at line 20 and the other at 45, both using the 'i' variable.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:You have two loops, one at line 20 and the other at 45, both using the 'i' variable.
Are they nested inside each other? If so, the compiler won't be at all happy. Yes, I think they are nested.

OP: You are going to have to learn all the rules about indentation. One of them is to make sure to put a bit of space before left {s. Look at lines 7 and 20. And never write }} (line 66). Another rule is that lines running off the right margin of the screen are wrong. Depending on which version of the forum software we use, such lines either disappear into cyberspace off the end of the screen and nobody can read the code, or in older versions they made the text so wide nobody could read your post without left & right scrolling and gave up reading after three lines. I shall try formatting your post so you can what it should look like, and so you can see the way to sort out long lines (next post).
Also don't write comments like // INITIALIZE ARRAYS or // output amount donated They don't tell us anything we can't see simply by looking at the code. They also don't tell us why you have created arrays of different length, the names array with ?? elements and the other arrays with 0 elements. You also shouldn't use ALL UPPER CASE TEXT.

As for your design. Are you really being taught to put so much code in the main method? Are you really being taught to use parallel arrays? That is a bad, non‑object‑oriented and error‑prone design.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what your line 12 should have looked like. There are various different ways I could have broken up the line:-Don't write code tags by hand: use the button. Don't use tabs for indentation: get your text editor to do it for you by converting 1 tab→4 spaces. If your text editor won't do that for you, get a decent text editor and stop using MS NotePad.
 
Carey Brown
Saloon Keeper
Posts: 3309
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Carey Brown wrote:You have two loops, one at line 20 and the other at 45, both using the 'i' variable.
Are they nested inside each other? If so, the compiler won't be at all happy. Yes, I think they are nested.


The compiler won't complain because only the first one declares 'i' to be an 'int' but the second one does not. So both loops end up using the same variable and the second loop clobbers 'i' as used by the first loop.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I was mistaken. Of course an error which gets past the compiler is much more serious than one which the compier picks up.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!