• Post Reply Bookmark Topic Watch Topic
  • New Topic

Object Reference Variable question  RSS feed

 
Renne Bautista
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey everyone,

I am currently reading through the 2nd edition of Head First Java. I finished the problems at the end of Chapter 3 and began to manipulate them, seeing what I could change while still yield the same output. My question pertains to the following two problems :

Problem 1 :


Problem 2 :


They both compile and run correctly. However, when I take : out of the while loop, in Problem 2, and place it under: the code does not compile? As apposed to the new objects created outside the while loop in Problem 1 which does compile?
 
Knute Snortum
Sheriff
Posts: 4081
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it's because the compiler knows that h[-1] is illegal, but I could be wrong. No, it compiles for me. What is the message you get?
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:I think it's because the compiler knows that h[-1] is illegal, but I could be wrong. No, it compiles for me. What is the message you get?

If it compiles for you, Knute, I suspect it would compile for Renne, too. My guess is that he introduced a typo that he hasn't spotted yet.

Renne, please post the message the compiler is giving you, like Knute requested, but also post the full code that is not compiling.
 
Renne Bautista
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stevens Miller wrote:
Knute Snortum wrote:I think it's because the compiler knows that h[-1] is illegal, but I could be wrong. No, it compiles for me. What is the message you get?

If it compiles for you, Knute, I suspect it would compile for Renne, too. My guess is that he introduced a typo that he hasn't spotted yet.

Renne, please post the message the compiler is giving you, like Knute requested, but also post the full code that is not compiling.


So I did find the error that was not allowing me to compile. After fixing it it now compiles, but when I attempt to run it I receive this error as follows :

Also, I am compiling and running through the terminal using bash.
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Renne, please post the full code that you are running. We can't help you if you don't.
 
Ahmed Bin S
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the array index starts at 0 and you cannot therefore store anything at position <0
 
Renne Bautista
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stevens Miller wrote:Renne, please post the full code that you are running. We can't help you if you don't.



Here is the original code from the book :


and this is the one I've altered (all i did was place the object reference outside of the while loop) :
 
Ahmed Bin S
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I told you in my previous post ...
 
Liutauras Vilda
Marshal
Posts: 4657
319
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Renne Bautista,

Apart from what has been mentioned already, there are more things to pay attention on:

1. Even tho, if this class is just for a test purpose, please keep in mind when you implement non-test, that instance variables in most cases should be private. Do differently if you have valid reason to do so.
2. Also, name variables so they'd reveal their purpose from the first glance. i.e. variables h, z are not descriptive and self explaining. Such things shouldn't be practiced even in test classes.
3. Don't really understand why you initialize z to -1? and in this matter using while loop which iterates in that way. Shouldn't be easier to use traditional form of for loop and iterate from 0 till ... ?
4. Each Hobbit you name as "bilbo", and later on if it turns out that it is 1 or 2 you rename it accordingly. That is also not very correct. Think of case how to name it once.
5. On lines 10 and 13 you have two if statements. Consider in such cases using else if. Bear in mind, it is going to be either 1 or 2, but never both.
6. Last thing. Try do not cram everything to a main method, as it suppose to start your application only.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!