• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

string index out of range

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)

 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Omg, hey thanks alot man. your help is much appreciated.

Cheers
 
Marshal
Posts: 79392
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ken, please note that I fixed another bug while you were replying.
 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://en.wikipedia.org/wiki/Luhn_algorithm
 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Credit card numbers don't necessarily have to be 16 characters.
 
It is an experimental device that will make my mind that most powerful force on earth! More powerful than this tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic