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
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”.
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;
}
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.
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:
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.