Ryan Rampersad
Thoughts, opinions, ideas and now links
  • About
  • Podcast
  • Links

Java: the left-hand side of an assignment must be a variable

February 29, 2012

After programming in Scheme for months, going back to Java can cause some problems as you can imagine. I encountered this error a few days ago while writing just the simplest searching loop.

public static void main(String[] args) {
	int[] arr = new int[] {2804, 2374, 6382, 1899};
	
	int index = 0, largest = arr[0];
	for (int i = 1; i < arr.length; i++) {
		(if largest > arr[i]) {
			index = i;
			largest = arr[i];
		}
	}
}

It’s your typical searching for the largest value loop. It’s nothing special, but then again, it doesn’t work.

At least the error message is logical. No, not really.

the left-hand side of an assignment must be a variable

By studying very carefully and fully realizing that I am still in scheme mode, I notice that there is a parenthesis before the if-statement instead after the if keyword itself. Had I been helping someone else with this code and they showed me that error message along with this, would I have known it was a misplaced parenthesis? No way. I’d eliminate one possibility at a time by removing each successive layer of expressions and assignments until it stopped. I might even go so far as to retype the entire thing. It’s subtle.

It also turns out that if you try to assign a value to a method, you’ll cause this error too.

Happy language switching!

Tagged: assignment, error, Java
<Previous Next>

© 2013 Ryan Rampersad.