FAQ: Learn Java: Loops - Using For Loops

This community-built FAQ covers the “Using For 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 Using For 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!

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

I commented out the while loop using /* */
I wanted to leave it there for comparison and to help with the creation of the for loop

5 Likes

Why does this fail the test yet the output is correct?

class Coffee {

  public static void main(String[] args) {

    // initialize cupsOfCoffee
    int cupsOfCoffee = 1;
    // add while loop with counter
    for (; cupsOfCoffee <= 100; cupsOfCoffee++) {
      System.out.println("Fry drinks cup of coffee #" + cupsOfCoffee);
    }
  }

}
1 Like

In all likelihood, the SCT for the lesson is using some form of regex test to see if your for loop looks like this:

for (int cupsOfCoffee = 1; cupsOfCoffee <= 100; cupsOfCoffee++)
2 Likes

I just tried to solve the exercise in 2 different ways (with the for-Loop) and it was incredibly frustrating seeing the Result being right, but me not being able to continue, because I used i instead of cupsOfCoffee and <101 instead of <= 100…

2 Likes

I agree. From a subjective viewpoint, the while loop begins at the “while” statement, not the initialization of the cupsOfCoffee integer variable.

2 Likes

this question has trash answer validation. check the output, not the actual code. this for loop could be written in so many different ways. just look for “for,” not the specific regex.

1 Like

I wanted to mention that I wrote my code as follows, which passed, although the output read “Fry drinks cup of coffee #1” 100 times. My initial reaction was, “WTF is going on here?” until I figured out my mistake. Can you find it? Hint: it’s in the for loop header.

class Coffee {

public static void main(String args) {

int cupsOfCoffee = 1;

for (int i = 1; i <= 100; i++) {
  System.out.println("Fry drinks cup of coffee #" + cupsOfCoffee);
}

}

}

This exercise did a good job of introducing for loop headers (whereas–I believe?–the concept was more swiftly glossed over in the introduction to javascript series).

1 Like

I wish this task / lesson had better answer validation!

There are several different ways to write the code for this exercise. This is how my solution looked…

public static void main(String[] args) {
    
    int cupsOfCoffee = 1;

    for (int i = 1; i <= 100; i++) {
       cupsOfCoffee = i;
       System.out.println("Fry drinks cup of coffee #" + cupsOfCoffee);
    }

Hi Michael,

I found the below method worked for me .

int cupsOfCoffee = 1;

for (int i = 1; i <= 100; i++) {
  cupsOfCoffee = i;
  System.out.println("Fry drinks cup of coffee #" + cupsOfCoffee);
}

That way, you are able to check for cupsOfCoffee which is 100 .

In the code, if you replace cupsOfCoffee with i , then cupsOfCoffee will still be 1 .

for (int i = 1; i <= 100; i++) {
  System.out.println("Fry drinks cup of coffee #" + i);
}

Cheers

the keyword is:

for (int i = 1; i <= 100; i++)

Man this lesson made me furious lol.
It can be written correctly so many ways but for some reason they want it like this:

class Coffee {
public static void main(String args) {

int cupsOfCoffee = 1;

for (int i = 1; i <= 100; i++) {
   cupsOfCoffee = i;
   System.out.println("Fry drinks cup of coffee #" + cupsOfCoffee);
}

}
}

1 Like

This should have better answer/code validation because there are multiple ways the code can be refactored and produce the correct results.