Before asking us, you should make a minimal effort to find out for yourself, because, after all, that is the whole point of the exercise.

First, make sure you understand exactly what you are being asked to do and how you can achieve it - this should be quite independent of any code. Preferably, write down the task and your solution on paper, in whatever way is clearest to you (diagrams, instruction steps in your own language, etc). When you are quite clear how the solution will work and why, translate it back into Java code. You may feel you're at this point already, but the code you wrote suggests not.

Once you've written the code, look over it carefully and correct any obvious errors. Next try compiling it and running it. The compiler will tell you about any syntax errors you need to fix. If it works when you run it, you're done. If it doesn't work, look at any error messages and try to figure out what they're telling you (Java error messages usually tell you exactly what is wrong and where).

If you are able to compile and run the code and it still doesn't do what you want, try stepping through it by hand, with pencil and paper, playing the role of the computer. With small pieces of code like this, you can usually spot logic errors very quickly (a few minutes or less).

If you still have a problem and you really can't figure it out, ask a question here.

You may think this is all a waste of time, in which case I have wasted both my time and your time. So be it, but the code you have written suggests you need to do it.

If I had eight hours to chop down a tree, I would spend 6 hours sharpening an axe...
Anonymous