Solved

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

Posted on 2011-09-27
8
253 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:newone2011
  • 4
  • 4
8 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36710798
please post more code - where you are calling it - is it a constructor?
It does not have any return type required for a method.
0
 

Author Comment

by:newone2011
ID: 36710925
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.
0
 

Author Comment

by:newone2011
ID: 36710932
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;
}
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36710951
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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 36710967

or add a check, say:
public static String modString(String s) {
if(s.length() == 0) return s; // I added this line
char c = s.charAt(s.length()-1); // getting error here
if (!Character.isDigit(c))
s = (c + s.substring(0, s.length()-1));
return s;
}
0
 

Author Comment

by:newone2011
ID: 36711088
sorry, here is the call:
telcoAPI.setSessionData(Constants.INFO_TOT_DUE, modString(XPathAPI.eval(doc, "/XMLInfo/Data/AccountInfo/TotDue").toString().trim()));
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36711111
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
0
 

Author Comment

by:newone2011
ID: 36711781
k, let me check
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
My project did see openJDK that I installed. What could be the problem 7 123
maven example error 3 52
Problem to start Neon 20 87
simple java question 3 44
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now