FAQ: Learn Java: Loops - Incrementing While Loops

This community-built FAQ covers the “Incrementing While Loops” exercise from the lesson “Learn Java: Loops”.

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

Learn Java

FAQs on the exercise Incrementing While Loops

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!

Third exercise says that The 1 in this statement should correspond with the current value of cupsOfCoffee . When cupsOfCoffee is 100 , this should be printed: “Fry drinks cup of coffee #100”.

I was trying:

while (cupsOfCoffee <= 100){
System.out.println(“Fry drinks cup of coffee #1”);
cupsOfCoffee++;
}
Which was obviously printing only “Fry drinks cup of coffee #1” , 100 times.

So I changed to:

while (cupsOfCoffee <= 100){

  System.out.println("Fry drinks cup of coffee " + cupsOfCoffee);

  cupsOfCoffee++; 

}

And this works! Increasing the fry drinks cup of coffee by 1 in every line. The exercise still says something is wrong??? I cant press next even though my code is right. Help?

Oh I found the problem, I was missing the #

System.out.println(“Fry drinks cup of coffee #” + cupsOfCoffee);

I feel dumb now.

1 Like

I think that it is fairly common to review your code character by character and even delete and retype every dot, comma, colon and semicolon. Commas may look sometimes like dots from a distance and they have caused odd errors for me sometimes.
:slightly_smiling_face:

For any Futurama freak: this exercise is a loop based on the 100 coffee futurama episode where Fry spend 300 USD in coffees and after the 100 coffee he can move fast as the flash (and then he saves the world hehe). You can make it funnier adding a final quote like System.out.println(“Fry is faster than flash”).

System.out.println(“Fry drinks cup of coffee #” + cupsOfCoffee);
what if I wrote this outside the while loop.

Try it yourself! What happens?

1 Like

I did exactly the samething!, I am really enjoying CodeAcademy but every now & then they ask you to do an exercise & have not covered all bases e.g the exercise clearly states print “Fry drinks cup of coffee #1 so of course this is what we will set to print because we dont know any better as new learners, good thing is you can view solution & it maybe a typo for the number 1 added after the # key.

it you print right it actualy :
System.out.println(“Fry drinks cup of coffee #” + cupsOfCoffee);
cupsOfCoffee++;

So I got everything correct and am able to advance, but Fry is drinking 101 cups, and I can’t figure out why!

class Coffee {
  
  public static void main(String[] args) {
    
    // initialize cupsOfCoffee
    int cupsOfCoffee = 1;
    // add while loop with counter
    while (cupsOfCoffee <= 100) {
       System.out.println("Fry drinks cup of coffee #" + cupsOfCoffee); 
      cupsOfCoffee++;
    }    
       System.out.println("Fry drinks cup of coffee #" + cupsOfCoffee);
  }
     
}

I juuuuuust figured it out, I didn’t need the " <= ", only the " < " since the loop will run til it hits 100. is that a correct assumption?

I have done it and the output looks

Output:
Fry drinks cup of coffee #101

why does it show 101?