String index out of range: does not make sense

I am trying to do the simplest thing and keep getting java.lang.StringIndexOutOfBoundsException: String index out of range:

I have a phone number which is a string with length 7. I know it is 7 because I can output the whole string and I even wrote code to tell me the length

integer j = $0.tele_phone_number.length();
$0.phone_len = j;

I can substring the first three digits no prioblem

$0.Phone_Num = $0.tele_phone_number.substring(0,3);

But when I try to substring the last four digits I get index out of range.

$0.Phone_Num = $0.tele_phone_number.substring(0,3) + “-” + $0.tele_phone_number.substring(3,4);

Any Ideas?? I have tried a number of approaches. I even tried to do it without substring.

string p1 = $0.tele_phone_number.left(3);
string p2 = $0.tele_phone_number.right(4);

Same error.

I am not new to substring. CloverETL users guide describes it as expected:

- string substring(string arg, integer fromIndex, integer length);

The substring(string, integer, integer) function accepts three arguments: the first is string and the other two are integers. The function takes the arguments and returns a substring of the defined length obtained from the original string by getting the length number of characters starting from the position defined by the second argument. Thus, substring(“text”, 1, 2) returns “ex”.

Thanks

When I tried your code I did not have any problems and I obtained required result. Maybe the problem is in your input data or in metadata. Can you please show us your input data and metadata and also full stackTrace?

Enclosed are the files I used for your code: InputFile, OutputFile and GraphString.grf.

Code I used is following:
function integer transform() {
$0.Field1 = $0.Field1.substring(0,3) + “-” + $0.Field1.substring(3,4);
return ALL;
}

Regards

Thanks for the reply. The input is from a database. I should mention that I am new to CloverETL. I am supporting a set of graphs that were developed by someone else so I’m trying to familiarize myself with the tool. This is not a critical issue but it just bugs me because it is such a simple thing and I don’t know why I get the error. More info in attached.

Thanks again

Hello,
all looks fine in your graph. I believe, that you get shorter string in the sprtele_phone_number, but not necessarily in the first record. Try the following transformation instead of original one:

//#CTL2

// Transforms input record into output record.
function integer transform() {
	$0.Student_ID = $0.spriden_id;
	$0.Prefix_NM = $0.spbpers_name_prefix;
	$0.First_NM = $0.spriden_first_name;
	$0.Middle_NM = $0.spriden_mi;
	$0.Last_NM = $0.spriden_last_name;
	$0.Suffix_NM = $0.spbpers_name_suffix;
	$0.Addr_1 = $0.spraddr_street_line1;
	$0.Addr_2 = $0.spraddr_street_line2;
	$0.City = $0.spraddr_city;
	$0.State = $0.spraddr_stat_code;
	$0.Postal_CD = $0.spraddr_zip;
	$0.Addr_Type = $0.spraddr_atyp_code;
	$0.PIDM = $0.apbcons_pidm;
	$0.Class = $0.apbcons_pref_clas;
	printErr(“Whole phone number:“ + $0.sprtele_phone_number);
	printErr(“Part 1:“ + sprtele_phone_number.substring(0,3));
	printErr(“Part 2:” + $0.sprtele_phone_number.substring(3,4));
	$0.Phone_Num = $0.sprtele_phone_area+"-"+$0.sprtele_phone_number.substring(0,3) + "-" + $0.sprtele_phone_number.substring(3,4);

	return ALL;
}

Then we will know exactly with which value the problem occurs.

Thank you. I did not know about the printErr() function. This helped me find the error. As you suspected in your first reply, there was a problem with the input data. Of six thousand rows there were three with bad data. I can add logic to check length before trying to substring.

Thanks again for your help