FAQ: Learn Java: Manipulating Variables - Compound Assignment Operators

This community-built FAQ covers the “Compound Assignment Operators” exercise from the lesson “Learn Java: Manipulating Variables”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Build Basic Android Apps with Java

Learn Java

FAQs on the exercise Compound Assignment Operators

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hey. So I have written this:
public class BakeSale {

public static void main(String args) {

int numCookies = 17;

int numCupcakes = 12;

// Now cookies are being sold. First I sell 3, then I sell half. 

int numCookies -= 3;

int numCookies /= 2;

// Let's see how many is left'

System.out.println(numCookies);

}

}

This produce errors:
BakeSale.java:6: error: ‘;’ expected
int numCookies -= 3;
^
BakeSale.java:7: error: ‘;’ expected
int numCookies /= 2;
^
2 errors

I think I’ve written everything just like the assignment explains. I even passed the assignment and can continue.

Why are there errors?

2 Likes

Welcome to the forums!

Once we declare the data type of a variable, we don’t need to do it again. Because you already indicated that numCookies and numCupcakes are ints, you should not do it again when you are manipulating them. What change do you need to make?

6 Likes

I am getting the same error message but I don’t want to change anything I would like to see the output.

I am getting similar errors as well even though it stated that I presented the code correctly.

My code is as follows:
public class BakeSale {

public static void main(String args) {

int numCookies = 17;

int numCookies -= 3;

int numCookies /= 2;

// Add your code above

System.out.println(numCookies);

}

}

Is there an error with the solution in the text editor?

Since “int numCookies” was already declared you don’t have to repeat yourself. Your getting an error because you need to remove the “int”.

3 Likes

Ah! Eureka! It worked! Thanks so much for the clarification :slightly_smiling_face:

This helped me. Thank you!

2 Likes

This helped me too. Thank You!

1 Like

very helpful. Thanks!

1 Like

Thanks! This did the trick. :smile:

1 Like

I feel strange about how this exercice is turned…
The class is named BakeSale so we can assume this class is about counting the sales of the bakery and we’re told that a customer bought 3 cookies so it’s 3 more cookies sold and the best would be to add those to the number of cookies sold.
I don’t really understand why you’ve turned it that way…

I think the intent of the exercise’s author was to use the BakeSale class to keep track of the inventory of available cookies. As mentioned in Step 1,

You are also in charge of keeping track of how many cookies there are at the bake sale. This value is represented by the variable numCookies.

numCookies is supposed to represent available cookies, not the number of cookies sold.

I can’t imagine that we don’t need anything to clarify the variable name. And since imagination is not the name of the game. It worked but I don’t understand.
public class BakeSale {
public static void main(String args) {
int numCookies = 17;
numCookies -= 3;
numCookies /= 2;

// Add your code above
System.out.println(numCookies);

When we made the declaration and initialization,

int numCookies = 17;

we already specified that numCookies is an integer.
Once we have declared the type of the variable, we can neither declare it again nor change the data type.

int numCookies = 17;
int numCookies = 10; // This will give error because we have already 
                    // declared numCookies earlier in the code.
int numCookies = 17;
double numCookies = 17; // This will also give error
int numCookies = 17;
numCookies = 10; // This is fine because we aren't declaring the variable again.
                 // We are just re-assigning a new value to the variable.
                 // The new value is of the same type as the type in our
                 // declaration, so there is no issue.
1 Like

Thank you mtrtmk! It was just difficult to expect that this would work. I appreciate your clarification.
numCookies = 10; // This is fine because we aren’t declaring the variable again.
// We are just re-assigning a new value to the variable.
// The new value is of the same type as the type in our
// declaration, so there is no issue.