# Simple coding question, 1 error

• April 12th, 2014, 01:35 PM
Extrocity
Simple coding question, 1 error
Im reading this book called "Head First Java" by Kathy Sierra and on page 20 it says this. This is the link to the page i am on and the problem i need to work.

this is the code i wrote.

Code :

```public class Practice { public static void main(String[] args){ int x = 3; while (x > 0) { if (x > 2) { System.out.print("a"); } if (x == 2) { System.out.print("b c"); } if (x == 1) { System.out.print("d"); x = x - 1; } x = x - 1; System.out.print("-"); } } }```

I am supposed to get the answer: a-b c-d

How is this?
• April 12th, 2014, 01:47 PM
sebbe605
Re: Simple coding question, 1 error
It's
Code :

`System.out.print("-");`

Put some breakpoints out and follow whats happens and im sure youll get a clearer picture of what's happening.

--- Update ---

Code :

```public class Practice {   public static void main(String[] args) { int x = 3; while (x > 0) {   if (x > 2) { System.out.print("a"); System.out.print("-"); }   if (x == 2) { System.out.print("b c"); System.out.print("-"); }   if (x == 1) { System.out.print("d"); x = x - 1; }   x = x - 1; //System.out.print("-"); } } }```

What you're doing is printing out a "-" every time in the while loop. You loop 3 times therefore you'll get the "-" 3 times as well. But like a said follow the debuggers it's a good way to learn.
• April 12th, 2014, 02:13 PM
Extrocity
Re: Simple coding question, 1 error
Is there another way to do it by re-organizing things? i was given blocks of code and told to organize them to get the answer.

x = x - 1;
System.out.print("-");

was one whole block of code that i had to place somewhere.
• April 12th, 2014, 03:58 PM
GregBrannon
Re: Simple coding question, 1 error

What are the assembled blocks of code supposed to do? We're a bit blind here.
• April 12th, 2014, 04:01 PM
sebbe605
Re: Simple coding question, 1 error

Also use code block when posting code, see FAQ!
• April 12th, 2014, 06:40 PM
sebbe605
Re: Simple coding question, 1 error
How did you get the block from the beginning?
• April 12th, 2014, 08:51 PM
Extrocity
Re: Simple coding question, 1 error
I updated the thread. Now it has the page of the book i am on that explains the dirrections. I also put my code in a code block. Hopefully someone can help me clear this up!

--- Update ---

I updated my thread, i got the code blocks from the page in the java book i am reading. I put a link to the page in my thread.