-2

I also the error "Left Hand Side of an Assignment must be a Variable" for following line

outPutArray.charAt(i)=inputArray[i]

how to resolve this? I have tried putting braces like but of no use

(outPutArray.charAt(i))=inputArray[i]
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Prit
  • 25
  • 1
  • 7

3 Answers3

1

You are trying to mutate a String object. This is not possible, String is supposed to be immutable. You have to create a new String with the changed content, use a char array instead, or something else that allows you to actually achieve what you are trying to do in the end (which is: given an input String, return an output String where one character is changed).

With that out of the way, you can't do an assignment on the return value of a function. You don't get a reference back that you can change and then some variable gets changed. You get a value back. The great Is Java "pass-by-reference" or "pass-by-value"? question is maybe a good read.

kutschkem
  • 7,826
  • 3
  • 21
  • 56
0

Very silly, but the format should be:

inputArray[i] = outPutArray.charAt(i);

not

outPutArray.charAt(i) = inputArray[i] 

putting the function on the left hand side means you are assigning a constant integer to a integer array element. As the integer is constant, it will fail. This is why we put our variables(inputArray[i] in your case) on the left hand side and our functions on the right hand(outPutArray.charAt(i)). Hope you understand!

codernaut1
  • 39
  • 7
  • 3
    `inputArray[i] = outPutArray.charAt(i);` to be fair, this is an assumption *you* are making. I mean, you are giving a syntactically correct piece of code, but you're assuming that's what OP is trying to do, which is not so obvious. – Federico klez Culloca Sep 22 '20 at 13:03
  • Additionally, where is the "constant integer" and "integer array element" coming from? – maloomeister Sep 22 '20 at 13:05
  • The constant integer is coming from the return value of ```outputarray.charAt(i)```. All numbers(0, 1, 2, 3, e.g.) are all constant, they do **not** change. You can make variables have that value, but you can't assign variables or constants to a constant. Once again: **they do not change**; – codernaut1 Sep 22 '20 at 13:17
  • @FedericoklezCulloca it is not a assumption. If it is a array, it is a array of variables. The variables can be a function, integer, float, even class! A function returns void, or a constant(1, 2, 3 for integer, 0.0, 0.1 for floats) – codernaut1 Sep 22 '20 at 13:21
  • @codernaut1 I believe maloomeister had a problem not with the "constant" part but with you calling them "integer", which is somehow correct (`char`s are integer numbers) but it's weird to use that term when `char` was right there for the taking. – Federico klez Culloca Sep 22 '20 at 13:21
  • @codernaut1 yours is an assumption because OP was trying to assign A to B and you assumed they were trying to assign B to A when it's much more likely they're just using the wrong syntax. – Federico klez Culloca Sep 22 '20 at 13:23
  • @FedericoklezCulloca Oh ok. @maloomeister, a char can be displayed as a character("a") or a integer through 0 and 255. You might see the integer form of a char by this ```javascript"\x00"```. – codernaut1 Sep 22 '20 at 13:31
  • @FedericoklezCulloca ill not try to get into a argument but ik it is a syntax error, wrong orientation of assignment – codernaut1 Sep 22 '20 at 13:32
  • 1
    @codernaut1 of course it is a syntax error while yours is *syntactically* correct, I'm not questioning that. What I'm questioning is your assumption that the code you provided is *semantically* correct. – Federico klez Culloca Sep 22 '20 at 13:34
  • @FedericoklezCulloca thank you for mentioning that. My explaining is not the best(proved by my parents and teachers). But let's close this argument ok? I will fix my answer later if that's fine. – codernaut1 Sep 22 '20 at 13:43
0

.charAt(i) function returns the character at position i. It does not return the reference to the character at that index. Look at this for more details

Maitreya Verma
  • 631
  • 4
  • 4