I think your problem may be more of a failure of logic than a failure of java. If I were you, I would attempt to formulate an algorithm to solve the problem in question then flesh it out in pseudocode then convert it to java syntax.

If the problem is that you don't understand how to actually write the code in java but you understand how to solve the problem, that would be a question that I'm sure your java teacher would be happy to answer.