File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Another Question from the book Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Another Question from the book "Head First Java"" Watch "Another Question from the book "Head First Java"" New topic
Author

Another Question from the book "Head First Java"

andrew dale
Ranch Hand

Joined: Aug 15, 2011
Posts: 45
Second question - Second post ---Sorry guys, I'm addicted to Java I wish I would've found out about this earlier in my life!


The Question in the book is called "Code Magnents" it gives you line of code in mixed up boxes, and your goal is to put them in the right way.

The answer when the code works is "a-b c-d"

My code does that but I had to change to Which I am pretty sure you're not supposed to do.

Anyways heres the full code, can you spot what I did wrong?



You'll never achieve your goals if you don't take that chance, so go pry open that trunk and get those amps.
Raymond Tong
Ranch Hand

Joined: Aug 15, 2010
Posts: 230
    
    2

your code gives "~a~b c~d" where you said you have got "a-b c-d"

Rearrange the following statement may give you different result
Daniel Vlad
Greenhorn

Joined: Aug 07, 2011
Posts: 29
Use int x=3 as it was and try it like this:


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

Or remove those 3 separate x = x - 1 statements and add one single x = x - 1 statement as the last statement of the loop, just after the third if-block.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Daniel Vlad
Greenhorn

Joined: Aug 07, 2011
Posts: 29
Rob Spoor wrote:Or remove those 3 separate x = x - 1 statements and add one single x = x - 1 statement as the last statement of the loop, just after the third if-block.


You posted right as I noticed that mistake. Sorry about that, Rob is right.
I had those x = x - 1 statements around because I was messing with the code, trying to do it in more than just one way, and i forgot to comment them out when i moved on to the other chapters.

I also edited my previous post(the actual code) to the way it was supposed to be when using the code magnets from the book.
andrew dale
Ranch Hand

Joined: Aug 15, 2011
Posts: 45
Hmm using NetBeans, you're code says build successful but, outprints nothing?
James Hughes
Greenhorn

Joined: Jan 14, 2014
Posts: 1
Sorry, I don't know how to post printscreens into this forum, but I came up with this arrangement which seemed to work:



Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38466
    
  23
Welcome to the Ranch

We don't like screenshots, but we do like code tags which I have added (and removed some excess blank lines) and doesn't it look a lot better.
By the way: nobody seems to have said which page that exercise is on.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Another Question from the book "Head First Java"