Reputation: 63
private void Keypad1ActionPerformed(java.awt.event.ActionEvent evt) {
int changestate = 10;
int keypadvalue = 1;
if(counterforlarge != 0){
if(smalldrinkcounter == 0){
smalldrinkcounter++;
} else {
smalldrinkcounter+changestate+keypadvalue;
}
} else {
counterforlarge++;
}
}
I do not understand why I am not able to add the three variables at second if statement. I have initialised all variables. All variables are ints.
I just don't understand this. Please let me know.
Upvotes: 2
Views: 111
Reputation: 5619
The left-hand side of an assignment must be a variable.
Suppose in your code, you write something like that
int changestate = 10; //line number 1
int keypadvalue = 1; //line number 2
1 + 2; //line number 3
You will get an error message for line number 3,
The left-hand side of an assignment must be a variable
Syntax error on token "+", invalid AssignmentOperator
Exactly same will happen for the following code,
smalldrinkcounter+changestate+keypadvalue; //The left-hand side... error
So you need to assign the summation result into a variable.
int somevariable = smalldrinkcounter+changestate+keypadvalue; //compile fine
I see there is another problem in your code,
if(smalldrinkcounter == 0){
smalldrinkcounter++;
} else {
smalldrinkcounter+changestate+keypadvalue;
}
Where you initialize smalldrinkcounter
if the else
block executed?
Upvotes: 1
Reputation: 1385
You are adding the 3 up, but you are not assigning them to a variable. Look at it like this: 1 + 1 = 2, and 2 = 1 + 1. But without the '=' there wouldn't be a result of 1 + 1. Same goes with this variables. If you only do something + something + something, without the '=', there wouldn't be an outcome. If you add: result = something + something, the result of the calculation would be stored in the result!
If you would want to assign the 3 to a variable called 'total' it would look like:
int total = smalldrinkcounter + changestate + keypadvalue;
If you would want the smalldrinkcounter to be the total of the 3, you could do:
smalldrinkcounter += changestate + keypadvalue;
Which is equal to: smalldrinkcounter = smalldrinkcounter + changestate + keypadvalue;
In the piece of code you provided, it's worth noting that counterforlarge and smalldrinkcounter are not declared and assigned anywhere, so the if/else statement wouldn't work properly. To let them work properly, you should add:
int smalldrinkcounter = 0;
int counterforlarge = 0;
to your code.
As a bit of extra info on assigning variables in your piece of code: basicly the statements smalldrinkcounter++
is the same as: smalldrinkcounter += 1;
which in turn is equal to: smalldrinkcounter = smalldrinkcounter + 1;
On a sidenote, if you are just starting with programming: Try to make your software as readable as possible. You could change your variable names with camelcases to make them more readible for example. They would look like smallDrinkCounter
and changeState
and keypadValue
.
Upvotes: 3
Reputation: 73
Where is the initilization of counterforlarge and smalldrinkcounter?
This statement
smalldrinkcounter+changestate+keypadvalue;
has no use, cause you do not save the result. Should be something like this
smalldrinkcounter += changestate+keypadvalue;
Upvotes: 2
Reputation: 55
in left-hand side there must be always a variable if you do an operation to be equal.
int all = smalldrinkcounter + changestate + keypadvalue;
Upvotes: 1
Reputation: 3292
Variable here = smalldrinkcounter+changestate+keypadvalue;
Thats your problem. Adding those variables is useless if not assigned to anything.
Upvotes: 0