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
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
ASKER
I am calling it from another method. The calling line is:
telcoAPI.setSessionData(Co nstants.IN FO_TOT_DUE , FixString(XPathAPI.eval(do c, "/XMLInfo/Data/AccountInfo /TotDue"). toString() .trim()));
It fails at this call.
telcoAPI.setSessionData(Co
It fails at this call.
ASKER
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;
}
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(Co nstants.IN FO_TOT_DUE , FixString(XPathAPI.eval(do c, "/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
You show the code where you are actually calling it
this does not have the call:
telcoAPI.setSessionData(Co
just printout the string which you are passing to it every time - you'll probably see when it is empty string
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
sorry, here is the call:
telcoAPI.setSessionData(Co nstants.IN FO_TOT_DUE , modString(XPathAPI.eval(do c, "/XMLInfo/Data/AccountInfo /TotDue"). toString() .trim()));
telcoAPI.setSessionData(Co
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
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
ASKER
k, let me check
It does not have any return type required for a method.