• Post Reply Bookmark Topic Watch Topic
  • New Topic

Output Issue  RSS feed

 
David R James
Greenhorn
Posts: 10
1
Android Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

I am slowly making my way through the Head First Java book. I've already solved 2 problems (totally hyped!) but I now have an exercise which I am really scratching my head about.

The exercise is called 'Code Magnets' and I have 7 snippets of code:















I have to put these 7 snippets of code in the right order. There are some curly braces missing in some code snippets just to add to the challenge.

The goal is to get the output:

a-b c-d

Here is my code that I have written:



My output is this:

a-b c-d-

For some reason I have an extra hyphen on the end of my output and I can't seem to figure out why. I have checked over the code a few times now and it seems the more I read and process the logic of it the more I start to slowly spiral into confusion.

As far as I can see there isn't any other way to order the code snippets without having a while loop paradox and I'm scared I might break the very fabric of space and time if that happens.

Any help is greatly appreciated!
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So how many hyphens are you looking for? Two? I haven't got my copy here at the moment, so I can't check the question.
Start by indenting your code correctly. Lines 11, 16, 21, 22, 25, and 26 ought to be one level farther to the right than you have them. Also delete the blank lines preceding those blocks. That will allow you better to see where the loops and ifs end.
Go through that code with a pencil (or fingertip) and as you go read out the current value of the int. I think you can get it to work by moving the sixth block of code as posted in small pieces.
 
David R James
Greenhorn
Posts: 10
1
Android Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Campbell Ritchie. Hey thanks for the advice!

I have tried moving the code snippet:



I've moved it outside of the while loop and tried it in all 3 if loops (it didn't really make sense to put it in there but I tried it anyway) and I still can't seem to get rid of that last hyphen.

@Campbell Ritchie. Yep there is only supposed to be two hyphens, one between the a and b and another between the c and d.

I did step through the code to get the value of x on each loop through the while loop and it all sounds logical to me, I really don't know why that last hyphen is there
 
João Victor Gomes
Ranch Hand
Posts: 113
11
Eclipse IDE Java Netbeans IDE Postgres Database Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you should try to decrease the x value and print the hyphen before printing the last letter (d).
 
David R James
Greenhorn
Posts: 10
1
Android Java MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
João Victor Gomes wrote:Maybe you should try to decrease the x value and print the hyphen before printing the last letter (d).


That solved it!

Thanks so much, this was really confusing me, all I had to do was print the hyphen before printing the last letter!



The output is now:

a-b c-d
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

What happens if you put the sixth code block at line 12 rather than 16?
 
João Victor Gomes
Ranch Hand
Posts: 113
11
Eclipse IDE Java Netbeans IDE Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch

What happens if you put the sixth code block at line 12 rather than 16?


Good point. It's important to understand how it worked.

Davey James, you should try what Campbell said, and analyze what's going on. See the value of x being changed to match the if's conditions and understand the sequence of prints.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!