Search code examples
javatalend

reference array using variable in Java


I please bear with me, I have been using Java for 2 days and i've hit a bit of a hurdle.

I am using Talend to perform a count using the tMemorize and tJava components but this may be a question for a Java developer. I have previously posted an issue with using this method within a joblet by my new issue is more Java related which can be viewed here: using Joblets in talend with tMemorize and tJavaFlex

I need to reference an array generated by the java code talend. I cannot reference this element directly because of an issue with using tJavaFlex within multiple joblets: Java renames joblets each time they are used.

It may be useful to understand how my code works in normal circumstances (excluding the use of joblets).

int counter = 1;

if (EnquiryID_mem_1_tMemorizeRows_1[0].equals(EnquiryID_mem_1_tMemorizeRows_1[1]))
{
counter++;
}
row3.counter = counter;

The EnquiryID_mem_1_tMemorizeRows_1[0] and EnquiryID_mem_1_tMemorizeRows_1[1] is what I need to reference.

To overcome this I have written the following code.

String string = currentComponent;
String[] parts = string.split("_");
String part1 = parts[0];
String part2 = parts[1];
String joblet = part1+'_'+part2;
String newrow = "EnquiryID_"+joblet+"_tMemorizeRows_1"

if (newrow[0].equals(newrow[1]))
{
counter++;
}
row3.counter = counter;

However I get the following error:

The type of the expression must be an array type but it resolved to String

I understand that the newrow variable is a string and I am using it to reference an array. I have searched far and wide online for a resolve but I cannot fine one. Can someone help me please?

Thank you

Here is the talend code that my code should reference. I have taken it from the currentComponent that I am using to when it changes to one not in use directly.

                        currentComponent = "mem_1_tMemorizeRows_1";

                    // row1
                    // row1

                    if (execStat) {
                        runStat.updateStatOnConnection("row1" + iterateId,
                                1, 1);
                    }

                    for (int i_mem_1_tMemorizeRows_1 = iRows_mem_1_tMemorizeRows_1 - 1; i_mem_1_tMemorizeRows_1 > 0; i_mem_1_tMemorizeRows_1--) {
                        EnquiryID_mem_1_tMemorizeRows_1[i_mem_1_tMemorizeRows_1] = EnquiryID_mem_1_tMemorizeRows_1[i_mem_1_tMemorizeRows_1 - 1];
                    }
                    EnquiryID_mem_1_tMemorizeRows_1[0] = row1.EnquiryID;

                    mem_1_row2 = row1;

                    tos_count_mem_1_tMemorizeRows_1++;

                    /**
                     * [mem_1_tMemorizeRows_1 main ] stop
                     */

                    /**
                     * [mem_1_tJavaFlex_1 main ] start
                     */

                    currentComponent = "mem_1_tJavaFlex_1";

                    // mem_1_row2
                    // mem_1_row2

                    if (execStat) {
                        runStat.updateStatOnConnection("mem_1_row2"
                                + iterateId, 1, 1);
                    }

                    mem_1_row3.QuoteID = mem_1_row2.QuoteID;
                    mem_1_row3.EnquiryID = mem_1_row2.EnquiryID;

                    if (EnquiryID_mem_1_tMemorizeRows_1[0]
                            .equals(EnquiryID_mem_1_tMemorizeRows_1[1])) {
                        rower++;
                    }
                    mem_1_row3.rower = rower;

                    tos_count_mem_1_tJavaFlex_1++;

                    /**
                     * [mem_1_tJavaFlex_1 main ] stop
                     */

                    /**
                     * [mem_1_tMap_1 main ] start
                     */

                    currentComponent = "mem_1_tMap_1";

Thank you to everyone who has helped so far.


Solution

  • This

    if (newrow[0].equals(newrow[1]))
    

    Tries to pick the first and second element of the array newrow. Unfortunately you declare newrow as

    String newrow = "EnquiryID_"+joblet+"_tMemorizeRows_1"
    

    which is not an array but a String. That syntax in the if will not work with a String. I am not sure what you are trying to do but that if check will not work.

    EDIT: If you are trying to pick up char from a string you need to use charAt(index).

    If you want to treat newrow as an array you have to declare it as such and pass appropriate elements to it.

    EDIT 2: I think you are trying to pass the actual data in joblet to newrow in this:

    String newrow = "EnquiryID_"+joblet+"_tMemorizeRows_1"
    

    But what happens here is that everything is concatenated in one String so you need to figure out where the data you are looking for (part[0] and part[1] I assume) is present in that String so you can pull them out (basically what indices contain the values you are looking for).

    An example of how newrow will look after that assignment:

    "EnquiryID_part1_part2_tMemorizeRows_1"
    

    So "part1" will start at index 10 and will end at index 14. I am just using "part1" here, but it would have whatever value is stored in part1 variable.

    If you can show us what you expect it to look like that would help.