Your logic needs work. You have some bugs in your code.
Try debugging the code by adding printlns to show the values of variables as they change and when they are compared to other variables. Look...