aspose file tools
The moose likes Beginning Java and the fly likes Book Answer vs JVM, Same Code Different Output? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Book Answer vs JVM, Same Code Different Output?" Watch "Book Answer vs JVM, Same Code Different Output?" New topic
Author

Book Answer vs JVM, Same Code Different Output?

Jerick Almonina
Greenhorn

Joined: Sep 02, 2008
Posts: 3
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

Joined: Mar 24, 2008
Posts: 3670

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 ]

SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32599
    
    4
And welcome to JavaRanch
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24041
    
  13

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.


[Jess in Action][AskingGoodQuestions]
Jerick Almonina
Greenhorn

Joined: Sep 02, 2008
Posts: 3
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
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Book Answer vs JVM, Same Code Different Output?
 
Similar Threads
Head First Java Code Magnets Chapter 3 Q
join()
Be the compiler excercise in chap 3
Head First Java Frustration!
Classpath setting involving java and javac