Solved

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

Posted on 2011-09-27
8
276 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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
 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
egit plugin on eclipse 8 100
jsp insert to database example 2 64
eclipse console opening separately 2 38
learn programming 8 70
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

733 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