This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Well, look at line 16. If x * 2 > 10 you decrease x by 9. That may cause x to become negative; after all, x * 2 > 10 && x < 9 is true for 6, 7 and 8 as values of x. Recognizing a Luhn-check, you want to change lines 12 to 21 to use don instead of x:
Note that I changed the 48 to '0'. They have the same value, but '0' is easier to read. The cast to int is not needed. Also, you need to add don always - you just need to modify it when x is even.
Quite a lot to comment about in that code. Please put spaces around your binary operators, which makes it easier to read. don’t use number literals like 48. You should avoid number literals altogether, if possible. It is not clear what 48 means. If you wish to subtract the digit 0 from it, you can do arithmetic on chars, with - '0'. That is much easier to read.
There is something odd about subtracting 9. If you start with the value 7, then twice 7 will be more than 10, so you subtract 9 and get -2 The JVM doesn’t like looking for the minus 2nd letter in a String.
May be you can add some code to check if the input string is of length 16 and also it contains only numbers by pattern matching .put that code in a while loop until the condition satisfies. use variable names like doubleTheNumber(instead of don) proceed with the rest of the code.
Had you used an IDE you would have got a warning like the local variable don never read