aspose file tools*
The moose likes Beginning Java and the fly likes A Puzzle...sort of Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A Puzzle...sort of" Watch "A Puzzle...sort of" New topic
Author

A Puzzle...sort of

Raymond Spiess III
Greenhorn

Joined: May 03, 2007
Posts: 4
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 ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Raymond Spiess III
Greenhorn

Joined: May 03, 2007
Posts: 4
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
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Raymond Spiess III
Greenhorn

Joined: May 03, 2007
Posts: 4
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

Joined: May 03, 2007
Posts: 4
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!

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A Puzzle...sort of