aspose file tools*
The moose likes Java in General and the fly likes string index out of range Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "string index out of range" Watch "string index out of range" New topic
Author

string index out of range

kenny koh
Greenhorn

Joined: Oct 11, 2011
Posts: 7
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

Joined: Oct 27, 2005
Posts: 19785
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
kenny koh
Greenhorn

Joined: Oct 11, 2011
Posts: 7
Omg, hey thanks alot man. your help is much appreciated.

Cheers
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Oct 27, 2005
Posts: 19785
    
  20

Ken, please note that I fixed another bug while you were replying.
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
what rule are you applying here to validate the given string? I don't understand the code
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

http://en.wikipedia.org/wiki/Luhn_algorithm
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
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

Joined: Oct 27, 2005
Posts: 19785
    
  20

Credit card numbers don't necessarily have to be 16 characters.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: string index out of range