Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Basics Perfecting the Prototype Censoring Words - Looping Until the Value Passes

Using do-while loop for age

I am trying to use the do-while loop for the age requirment, so that the user will be prompted to enter a valid age before they continue. It works fine, except that when a valid age is entered, the message 'Sorry, you're not old enough" appears before running the next part of the code. Can anyone see what I am doing wrong?

String ageAsString;
      int age;
      boolean invalidAge;
      do {
        ageAsString = console.readLine("How old are you?  ");
        age = Integer.parseInt(ageAsString);
        if(invalidAge); {
          console.printf("Sorry, you are not old enough. Enter another age  ");
      } while(invalidAge);

3 Answers

I'm having the same issue with another do-while loop, but the only thing wrong I can see in yours is the ; after if(invalidAge).

Thanks for the feedback Isiah, I'll let you know if I ever find a solution

luis martinez
luis martinez
2,480 Points

take out the (;) after the if statement

       if(invalidAge) {
      console.printf("Sorry, you are not old enough. Enter another age  ");
  } while(invalidAge);

This might work(at least it did for me):

On the left of this page, press Workspaces. Then, find the workspace you are using. On the far right side of your workspace, there is a gear. Press the gear, then press Update Workspace. It should work after that.