Link to home
Start Free TrialLog in
Avatar of newone2011
newone2011

asked on

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

public modString(String s) {
char c = s.charAt(s.length()-1); // getting error here
if (!Character.isDigit(c))
s = (c + s.substring(0, s.length()-1));
return s;
}
I am getting the error here  when I pass String in method above:   +00000000000

I am
Avatar of for_yan
for_yan
Flag of United States of America image

please post more code - where you are calling it - is it a constructor?
It does not have any return type required for a method.
Avatar of newone2011
newone2011

ASKER

I am calling it from another method.  The calling line is:

      telcoAPI.setSessionData(Constants.INFO_TOT_DUE, FixString(XPathAPI.eval(doc, "/XMLInfo/Data/AccountInfo/TotDue").toString().trim()));

It fails at this call.
also yes it returns string:
public static String modString(String s) {
char c = s.charAt(s.length()-1); // getting error here
if (!Character.isDigit(c))
s = (c + s.substring(0, s.length()-1));
return s;
}
I think you at some point pass it an empty string  - just string with zero charcaters ""

You show the code where you are actually calling it

this does not have the call:
 telcoAPI.setSessionData(Constants.INFO_TOT_DUE, FixString(XPathAPI.eval(doc, "/XMLInfo/Data/AccountInfo/TotDue").toString().trim()));

just printout the string which you are passing to it every time - you'll probably see when it is empty string
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sorry, here is the call:
telcoAPI.setSessionData(Constants.INFO_TOT_DUE, modString(XPathAPI.eval(doc, "/XMLInfo/Data/AccountInfo/TotDue").toString().trim()));
well, it is very probable that XPathAPI.eval(doc, "/XMLInfo/Data/AccountInfo/TotDue").toString() returns something like just spaces and when you do trim()
with it it just becomes empty string "" (nothing between the quotes) and then you'll get this exception.
So modify it the way i suggested - check for the string length - and if it is zero - don't use this char c = s.charAt(s.length()-1);
just return the same empty string or do somthing else , but don't call this method, which will definitely cayuse problems in this case
k, let me check