• Post Reply Bookmark Topic Watch Topic
  • New Topic

Book Answer vs JVM, Same Code Different Output?  RSS feed

 
Jerick Almonina
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm a newly registered member here, I don't know where to post this post but for a try, I wrote this. Kindly correct me if my my post particularly this one is inappropriate for this room(General Java).

I'm reading the Head First Java book and I'm on chapter 3 already(Know your Variables), as I finish the required exercises( the one with the shoe icon),I check what I've done in contrast to the answer key and it is right, but when I try to actually run the code it turns out to be wrong because of the ouput.

This is the code the I've Done(From the Code Magnet):



when i ran the above code, looks like this:
Island = Conzumel
Island = Bermuda
Island = Bermuda
Island = Azores

It is Suppose to output like this:
isLand = Fiji
isLand = Cozumel
isLand = Bermuda
isLand = Azores


My JVM version is:
java version "1.6.0_07"
Java(TM) SE Runtime Environment (build 1.6.0_07-b06)
Java HotSpot(TM) Client VM (build 10.0-b23, mixed mode, sharing)


Is there a problem with the book or the compiler or jvm?
 
Vijitha Kumara
Bartender
Posts: 4002
42
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nothing wrong with the output you are getting. That is the output according to the code you are given.



index[1] value will be 0. Since you are not assigning anything to that position (It defaults to 0 because the array type is int). So when y's value is 1 & 2 it will output --> Bermuda (index[1]/index[2] -->0), So ref--> 0 and island[0] --> Bermuda

Check the value of ref variable in each iteration. Then you will see this.

Hope this helps.
[ September 02, 2008: Message edited by: Vijitha Kumara ]
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to JavaRanch
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suspect you copied the code wrong, or the book has a typo; the line

index[0] = 3;

is probably supposed to be

index[1] = 3;

which would then give the right output.
 
Jerick Almonina
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I got it, I missed typed the code in contrast to my writing on my notes. Thank for helping out guys.

I'm just new here, I took your advices as a warm welcome here. Next time I'll be keen thanks again guys
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!