aspose file tools*
The moose likes Beginning Java and the fly likes StringIndexOutOfBoundsException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "StringIndexOutOfBoundsException" Watch "StringIndexOutOfBoundsException" New topic
Author

StringIndexOutOfBoundsException

Tee Morales
Greenhorn

Joined: Apr 21, 2009
Posts: 3
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

Joined: Feb 10, 2009
Posts: 710
I would suggest looking here and here. An important thing to note is the the setup of the substring method:


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
What would "billingUnit = record.substring(9,4);" return to billingUnit?


SCJA
~Currently preparing for SCJP6
Prabz Bhatia
Greenhorn

Joined: Apr 14, 2009
Posts: 19
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

Joined: Apr 21, 2009
Posts: 3
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

Joined: Oct 13, 2005
Posts: 36566
    
  16
All sorted out now, so . . . welcome to JavaRanch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: StringIndexOutOfBoundsException
 
Similar Threads
Help Parsing a file
error
strange searching
taking first four numbers from a set of number
.append() method