Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

StringIndexOutOfBoundsException

 
Tee Morales
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Getting this error. I ran it through the debugger and I see that it is taking an error on the second field billingUnit = record.substring(9,4);. See how the String is greater than the substring I am indexing. I don't understand how it is taking such an error.


10:22:24.158 COL I main: *************** Exception occurred **************
10:22:24.158 COL I main: toString: java.lang.StringIndexOutOfBoundsException: String index out of range: -5
10:22:24.158 COL I main:
10:22:24.158 COL I main: getMessage: String index out of range: -5
10:22:24.158 COL I main:
10:22:24.158 COL I main: StackTrace:
java.lang.StringIndexOutOfBoundsException: String index out of range: -5


groupSORDateExtract = new GroupSORDate(detailSORExtract);


public class GroupSORDate {

private String group = null;
private String billingUnit = null;
private String accountNumber = null;
private String enrollmentDate = null;
private String billingDate = null;
private String status = null;


public GroupSORDate(String record) {
group = record.substring(0,9);
billingUnit = record.substring(9,4);
accountNumber = record.substring(13,7);
enrollmentDate = record.substring(20,8);
billingDate = record.substring(28,8);
status = record.substring(36);
}

Record:

tctttt tttttctttt 0101200904202009R




 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest looking here and here. An important thing to note is the the setup of the substring method:
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What would "billingUnit = record.substring(9,4);" return to billingUnit?
 
Prabz Bhatia
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tee Morales wrote:
billingUnit = record.substring(9,4);
accountNumber = record.substring(13,7);
enrollmentDate = record.substring(20,8);
billingDate = record.substring(28,8);



the problem lies right here:
startindex>endindex

The syntax of the substring function reads:

public String substring(int beginIndex,
int endIndex)
Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.




Hope this helps...
 
Tee Morales
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!

I goofed up.


group = record.substring(0,9).trim();
billingUnit = record.substring(9,13).trim();
accountNumber = record.substring(13,20).trim();
enrollmentDate = record.substring(20,28).trim();
billingDate = record.substring(28,36).trim();
status = record.substring(36);
}
 
Campbell Ritchie
Sheriff
Pie
Posts: 48954
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All sorted out now, so . . . welcome to JavaRanch
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic