Solved

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

Posted on 2011-09-27
8
240 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

708 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

18 Experts available now in Live!

Get 1:1 Help Now