File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

string index out of range

 
kenny koh
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to all helpful ppl, i am writing this code and getting an error. java.lang.StringIndexOutOfBoundsException: String index out of range: -2.
at java.lang.String.charAt(String.java:695)

 
Rob Spoor
Sheriff
Pie
Posts: 20381
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
kenny koh
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Omg, hey thanks alot man. your help is much appreciated.

Cheers
 
Campbell Ritchie
Sheriff
Pie
Posts: 47288
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

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.
 
Rob Spoor
Sheriff
Pie
Posts: 20381
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ken, please note that I fixed another bug while you were replying.
 
Harsha Smith
Ranch Hand
Posts: 287
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what rule are you applying here to validate the given string? I don't understand the code
 
Rob Spoor
Sheriff
Pie
Posts: 20381
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://en.wikipedia.org/wiki/Luhn_algorithm
 
Harsha Smith
Ranch Hand
Posts: 287
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Spoor
Sheriff
Pie
Posts: 20381
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Credit card numbers don't necessarily have to be 16 characters.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic