TaeHyun. A TaeHyun. A - 1 year ago 134
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>();

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
may change)

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download