• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

StringIndexOutOfBoundsException

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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




 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What would "billingUnit = record.substring(9,4);" return to billingUnit?
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
 
Marshal
Posts: 80267
430
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All sorted out now, so . . . welcome to JavaRanch
reply
    Bookmark Topic Watch Topic
  • New Topic