Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

A Puzzle...sort of

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am really new to java. I just bought a book that I am going through and there is a puzzle to solve. I have the solution but my output does not match the output the book says it should be. Anyone care to go over this and help explain to me if the book is wrong?

The output should be:
%java EchoTestDrive
helloooo...
helloooo...
helloooo...
helloooo...
10

But I get 11. What am I doing wrong?


EDIT by mw: Added Code Tags.
[ May 03, 2007: Message edited by: marc weber ]
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

The code you've shown here won't actually compile, because of the line

e1.count = count + 1;

There's no variable "count" in EchoTestDrive.main(), and the compiler will tell you that. If you change this line to

e1.count = e1.count + 1;

it will, indeed, compile, and give the right answer. If you ran a program and got 11, then you must have made this compile by changing something, just not the right thing, and running that changed program.
 
Raymond Spiess III
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, that correction does makes sense. However, I have only gone through the code in my head, I haven't yet compiled anything. My main concern is why do I get 11 when the book says it should be 10. I guess I am trying to follow the logic in my head and can't see how the answer is achieved.

Regards
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is your code with some println statements to help keep track of how the values change. (To run the logic in your head, make a mental note of each println. )

[ May 03, 2007: Message edited by: marc weber ]
 
Raymond Spiess III
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks! This is my first post but already I have learned quite a bit. Not the least of which is the "code" tags. Here is the section that I don't understand. Why doesn't this if statement get processed when x = 3, thus making e2.count = 11?

 
Raymond Spiess III
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found my error and just answered my own question. In my head I was processing this statement as true when x = 0 (obviously wrong), which threw off all the other calculations. Doh!

Thanks for everyone's help!

 
reply
    Bookmark Topic Watch Topic
  • New Topic