0

I have an arrayList of strings in my JDBC program, and I want to access them in order to add the contents of different columns to individual strings, but the concat method is not working for my strings, for whatever reason.

while ( project_set.next()){
    for (int i=3;i<=meta.getColumnCount();i++){
        int arr_pos = i-3;
        String temp =project_set.getString(i) + "\n";
        project_data.get(arr_pos).concat(temp);
    }
}

I simplified it a lot for the purposes of this question, but temp stores the data from a column, and then I am trying to concatenate that to a spot in the arrayList, which has already been filled by an empty string.

0

2 Answers 2

3

String#concat returns a new String rather than modifying the original one as Strings are immutable. Instead set the List element to the result of concat:

project_data.set(arr_pos, project_data.get(arr_pos).concat(temp));

Aside: Use camelCase for variable names as defined in the Java naming conventions - e.g. projectData

Sign up to request clarification or add additional context in comments.

1 Comment

still having trouble....that did work, but now I am using a struct and having trouble doing the same thing to the projectWork field, which is a string. String temp =projectSet.getString(i) + "\n"; String temp2 = projectData.get(arrPos).projectWork.concat(temp); projectData.get(arrPos).projectWork = temp2; projectData.get(arrPos).projectName = meta.getColumnName(i);
0

Strings are immutable, calling concat returns a new String. You would need to set temp to be the concatentation of your data from the column and the current value in the arrayList, then call ArrayList.set(index, temp) to store the new value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.