Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Trouble when I try to do without using a compound assignment operator

  1. #1
    Member
    Join Date
    Dec 2018
    Location
    Wisconsin
    Posts
    54
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Trouble when I try to do without using a compound assignment operator

    I'm encountering some unexpected error messages when I try to do without a compound assignment operator. Here's my code:

    package samsExperiments;
     
    public class SamsArrays {	
     
    	public int getAverage(int[] arr) {
    		int sum = 0;
    		for(int x : arr) {
    			sum + x = sum;
    			(sum + x) = sum;
    			sum += x;
    		}
    	}	
    }

    Line 8 error: "Syntax error on token "+", invalid AssignmentOperator"

    Line 9 error: "The left-hand side of an assignment must be a variable"

    What's wrong? Why does it see an addition symbol as an assignment operator, when the = sign is clearly on the right-hand side of the x variable? And since sum and x are obviously both variables, why is it saying they're not?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Trouble when I try to do without using a compound assignment operator

    Look at the tutorial re the assignment statement: https://docs.oracle.com/javase/tutor...bolts/op1.html

    Basically an assignment statement has a variable to the left of the = and a value to the right.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. what is mean to compound assignment
    By ohad in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 11th, 2013, 08:10 AM
  2. The += assignment operator?
    By jean28 in forum Java Theory & Questions
    Replies: 2
    Last Post: January 19th, 2013, 01:03 AM
  3. Trouble with bounding values Assignment
    By rob17 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 13th, 2012, 12:38 AM
  4. another assignment from beginner programmer trouble
    By scottey313 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 30th, 2011, 01:13 PM
  5. Remainder Assignment Operator Help
    By jsam0123 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 5th, 2011, 06:04 PM