• Post Reply Bookmark Topic Watch Topic
  • New Topic

Stuck on something with arrays  RSS feed

 
Ryan Jammes
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello! I am doing an exercise which provides code snippets that one must piece together. I thought i had it the first time, but after i ran it my output was:


Island = Bermuda
y = 0
Index[y] = 0
ref = 0
Island = Bermuda
y = 1
Index[y] = 0
ref = 0
Island = Bermuda
y = 2
Index[y] = 0
ref = 0
Island = Bermuda
y = 3
Index[y] = 0
ref = 0


As the book just introduced these things, i have no idea why ref = index[y]; isn't correctly using y as it's index position. The output shows that y is incrementing nicely, but 'ref' and 'index[y] aren't. I looked it over a few times, but i am probably still missed something simple.. *sigh*

Full Code:


In the while loop, i am assigning 'index[y]' to 'ref'. I then output various strings and increment 'y' by 1. So shouldn't index -> [y] <- = 0, 1, 2, 3 with each pass through the loop? Which 'islands' should then use 'ref' at the appropriate times in the loop as well, resulting in correct output of the islands[1-4].
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

this line create the array 'index' and it is of type int, but index[0], index[1], index[2], and index[3] are 0. So when you reference index, you are only ever loading a 0 into your variable ref that you use for indexing your arrays. Put 0, 1, 2, and 3 into index this way:

I believe that will then do what you are looking for it to do.
 
Ryan Jammes
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:
this line create the array 'index' and it is of type int, but index[0], index[1], index[2], and index[3] are 0. So when you reference index, you are only ever loading a 0 into your variable ref that you use for indexing your arrays. Put 0, 1, 2, and 3 into index this way:

I believe that will then do what you are looking for it to do.


Thank you. I am super embarrassed to admit that i actually just missed that particular code snippet in the book.. I checked, re-checked and was so sure of myself, but nope. I am just an idiot that can't read apparently lol.. fml right?
 
Campbell Ritchie
Marshal
Posts: 55770
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Jammes wrote:. . . fml right?
No, wrong. Lots of people make that sort of mistake; it is not at all evidence of stupidity. Only of inexperience.
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan,
In college we used to call those 2 o'clock in the morning errors, and believe me, I have made my share and then some. When you learn the debugger it will help a lot in these types of cases, to actually step through the code and see that values are never assigned as originally expected, but the thing that I do, I program in may different languages, is to explicitly initialize each and every variable when I declare them. I never let expectation defaults ease my thoughts. Since my practice is to manually set every variable, I have very few instances of this.

Les

BTW: I still have to put code away, and then come back to it with fresh eyes. As already stated: experience will teach you ways to catch the 2 o'clock in the morning errors--we all make them.
Ryan Jammes wrote:
Les Morgan wrote:
this line create the array 'index' and it is of type int, but index[0], index[1], index[2], and index[3] are 0. So when you reference index, you are only ever loading a 0 into your variable ref that you use for indexing your arrays. Put 0, 1, 2, and 3 into index this way:

I believe that will then do what you are looking for it to do.


Thank you. I am super embarrassed to admit that i actually just missed that particular code snippet in the book.. I checked, re-checked and was so sure of myself, but nope. I am just an idiot that can't read apparently lol.. fml right?
 
Campbell Ritchie
Marshal
Posts: 55770
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe you should do the same for your islands array.That is called an array initialiser; you can do that any time you want to re‑assign the array but you will have to add new String[] to its start.It is more important for arrays of reference types.Remember nulls can be dangerous; if you try to use the elements of that array they will not exist and execution may fail with an exception. Using an array initialiser immediately after declaration (when you can omit new String[]) can remove all the nulls.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!