aspose file tools*
The moose likes Java in General and the fly likes java.lang.StringIndexOutOfBoundsException: String index out of range: -2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "java.lang.StringIndexOutOfBoundsException: String index out of range: -2" Watch "java.lang.StringIndexOutOfBoundsException: String index out of range: -2" New topic
Author

java.lang.StringIndexOutOfBoundsException: String index out of range: -2

Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 726
Hello,




This is my remove comma method


Line 6 of the code is throwing this error

Thanks & best regards
Sankar Mitra
Greenhorn

Joined: Oct 24, 2011
Posts: 4

It is possible only when the String str does not contain any character.Since it is empty its length is 0 and you are trying to go to its -2 position which is out of bounds.
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 726
Thanks for your reply.

How can I fix it?
Claudiu Chelemen
Ranch Hand

Joined: Mar 25, 2011
Posts: 71

Try checking the string's length before calling substring.
If it is long enough, call the substring, otherwise leave it as it is.

Claudiu
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 726
Thanks from the bottom of my heart. Now its working fine

Best regards
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
Never use ==true or ==false. It should be

... if (isCat) ...
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 726
thanks for the tip
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.lang.StringIndexOutOfBoundsException: String index out of range: -2
 
Similar Threads
switch doubt..
need help with 2 bugs
NullPointerException
PagingScrollTable Javascript error
xsl - sorting multiple fields