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

Resolved

Hello fellow Treehousers! I tried to implement a loop on an age requirement and I've been playing around with some code. The issue that even if you user does not meet the requirement of the program, the program still proceeds and the user is not prompted to meet the age requirement again before proceeding. Here is the code. I'm open to any feedback on areas I can also improve. I've add comments in areas I think I need some serious help in. Thank you Treehousers ^_^

package treehouse1;

import java.util.Scanner;

public class TreeHouse1 {

    public static void main(String[] args) {


     Scanner input = new Scanner(System.in); 

     String ageAsString;


    do{                  
   //Do I need this do here?

    System.out.printf("How old are you?");

    ageAsString = input.nextLine( );


     int age = Integer.parseInt(ageAsString);

     if ( age <18) 
    //Is this if doing anything? xD



 System.out.printf("Sorry you must be at least 18 to use this program.\n");


     }while (false); 
// I seem to be using this while wrong, can anyone shed light pls and ty 


     String firstName; 
     String adjective;
     String noun;
     String adverb;
     String verb;
     //adding a bunch of strings




     System.out.printf("What is your name?:");
     firstName = input.nextLine( );
     System.out.printf("Enter an adjective: ");
     adjective = input.nextLine( );
     boolean isInvalidWord;

     do{
      System.out.printf("Enter a noun:");
      noun = input.nextLine( );
       isInvalidWord =   (noun.equalsIgnoreCase("nerd")||                
                          noun.equalsIgnoreCase("dork")||
                          noun.equalsIgnoreCase("moron"));

      if (isInvalidWord) {


     System.out.printf("That language is now allowed. Try Again. \n\n"); 

     }
    } while (isInvalidWord);


     System.out.printf("Enter an adverb: ");
     adverb = input.nextLine( );
     System.out.printf("Enter a verb ending with -ing ");
     verb = input.nextLine( );




    System.out.printf("Hello, my name is %s!\n", firstName);   
    System.out.printf("%s is a %s %s.\n", firstName, adjective,noun);
    System.out.printf("%s is always %s %s.\n", firstName, adverb, verb);


    }



}

2 Answers

Caleb Kleveter Hey Caleb thanks for answering my question I actually already solved it I needed to make a int outside of my code because the String couldn't become a boolean, I pretty muched changed it so now it looks like this but thanks for your response

package treehouse1;

import java.util.Scanner;

public class TreeHouse1 {

    public static void main(String[] args) {


     Scanner input = new Scanner(System.in); 



     String ageAsString;
     int age;



    do{

    System.out.printf("How old are you?");


    ageAsString = input.nextLine( );



     age = Integer.parseInt(ageAsString);


     if ( age <18) 






         System.out.printf("Sorry you must be at least 18 to use this program.\n");



     }while (age < 18); //bundle of code ends here

That's great! I marked your answer as accepted because it solves your problem. If you ever solve an issue on your own you can accept your own answer. I also added proper code formatting with Markdown. Here is how to do it:

```language
// Code here
```

This part in your do-while loop:

if ( age <18) 
    //Is this if doing anything? xD

 System.out.printf("Sorry you must be at least 18 to use this program.\n");

If I am correct you are missing the braces:

if ( age <18) {
     System.out.printf("Sorry you must be at least 18 to use this program.\n");
}