String
is an immutable type, you can't assign to characters of a string and in this particular case you can't use a method as a left-hand side of an assignment operator.
str.charAt(..) =
makes no sense since you can't assign to a value returned from a method in Java. This would work in other languages, for example C++, where you can return a char&
from a method, but in Java you will always find something like void setCharAt(int index, char value)
(which doesn't exist, it is just to explain the problem).
Indeed check the error:
required: variable, found: value
You are trying to assign to a value, which is illegal, you must assign to a variable.
Just convert the String
to a char[]
through
char[] data = str.toCharArray();
data[i+1] = data[i];
so that you are free to do what you need.