Georgi Tonchev
Georgi Tonchev

Reputation: 35

Invalid Assignment Operator java

Here is my code:

h[ht] * sth -= 3;   

the " * " gives me an error:

Syntax error on token "*", invalid AssignmentOperator

I need the value of h[ht]*sth to be reduced by 3

Upvotes: 0

Views: 3444

Answers (2)

Arvind Bishnoi
Arvind Bishnoi

Reputation: 49

what you are trying is syntactically incorrect.

I suppose h[ht] and sth are two different variables and you want to reduce their result by 3.

basically this is Compile time error, therefore this not able to compile. Right?

in order to achieve this you need to break it into two different statements i.e.

int/long/float/double temp =  h[ht] * sth;
temp-=3;

or you can achieve this like this also ( h[ht] * sth )-3

and you will able to achieve what you want to do.

Please read here. and let me tell you Java's compiler is implemented using this grammar, therefore if any statement is not following these grammatical rules, result into syntactical error.

I hope your issue will be resolved. Thank you

Upvotes: 0

nablex
nablex

Reputation: 4757

Your example is invalid because "-=" is actually a contracted form:

int a = 1;
a -= 1;
// the above line is the same as:
a = a - 1

Assign the computation to something else:

int a = (h[ht] * sth) - 3

Upvotes: 2

Related Questions