Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.