[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2011-09-27
8
Medium Priority
?
330 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
Independent Software Vendors: 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!

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This video teaches viewers about errors in exception handling.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

650 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