TaeHyun. A TaeHyun. A - 1 month ago 5
Java Question

How to combine multiple strings into one by using `=+` operator? - Java

I have a set of strings in an ArrayList and an empty String variable:

String combination;

ArrayList<String> sList = new ArrayList<String>();
sList.add("H");
sList.add("e");
sList.add("l");
sList.add("l");
sList.add("o");


I am trying to combine all the letters so that it would print "Hello".

I tried using a for loop like this:

for (int x = 0; x < sList.size(); x++) {

combination =+ sList.get(x);
}


But when I try to compile this, it says:
error: bad operand type String for unary operator '+++'


How can I combine these letters with a loop?

(Note that the size of
sList
may change)

Answer

Change: combination =+ sList.get(x); to combination +=sList.get(x); or do combination =combination+sList.get(x);

Comments