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

Randolph Wiafe
Randolph Wiafe
1,288 Points

Do While loop ends after 3 tries. Why is that?

So after entering all three isInvalidWord it tells me to Enter adverb. Does this have anything to do with how many words we have in isInvalidWord?

Enter noun: jerk
That language is not allowed. Try again.
Enter noun: dork
That language is not allowed. Try again.
Enter noun: nerd
That language is not allowed. Try again.
Enter adverb:

Code below in case that helps.

public class TreeStory {

public static void main(String[] args) {
  Console console = System.console();
  // Below is a multi line comment
  /*  Some terms:
      noun - Person, place or thing
      verb - An action
      adjective - A description used to modify or describe a noun
      Enter your amazing code here! 
  */
  // __Name__ is a __adjective__ __noun__. They are always __adverb__ __verb__.

  // Here we convert a string to integer using the Integer class
  String ageAsString = console.readLine("How old are you? ");
  int age = Integer.parseInt(ageAsString);

  // If age is greater than 13 contiune else exit
  if (age > 13) {
    String name = console.readLine("Enter name: ");
    String adjective = console.readLine("Enter adjective: ");
    String noun;
    boolean isInvalidWord;
    // Do while Loop
    // Remember to declare variable outside of Do scope
    do {
      noun = console.readLine("Enter noun: ");
      isInvalidWord = noun.equalsIgnoreCase("dork") || noun.equalsIgnoreCase("jerk") || noun.equalsIgnoreCase("nerd");
      if (isInvalidWord) {
        console.printf("That language is not allowed. Try again.\n");
      }
    } while (noun.equalsIgnoreCase("dork") || noun.equalsIgnoreCase("jerk"));

    String adverb = console.readLine("Enter adverb: ");
    String verb = console.readLine("Enter verb: ");

    console.printf("Tree Story \n --------------- \n");
    console.printf("%s is a(n) %s %s. They are always %s %s.\n", name, adjective, noun, adverb, verb);
  } else {
    console.printf("You're not old enough!!!\n");
    System.exit(0);
  }
}

}

samuel zaffran
samuel zaffran
24,815 Points

In your condition (while) you just asked to loop if the noun is equal to jerk or dork but not nerd, so when you enter nerd it's no longer a condition valable and it's get out out the loop... Not about the number of time your enter a noun !! Hope this will help !

Randolph Wiafe
Randolph Wiafe
1,288 Points

samuel zaffran Ah because the while is what makes it loop! Thank you very much!

samuel zaffran
samuel zaffran
24,815 Points

You're well come ! Of course, do something while a condition is checked, if the condition is no longer checked do not accomplish the code in the do section, simple as that ;) (I 'll just post the same answer in the answer section, if someone'll need it in the future)

1 Answer

samuel zaffran
samuel zaffran
24,815 Points

In your condition (while) you just asked to loop if the noun is equal to jerk or dork but not nerd, so when you enter nerd it's no longer a condition valable and it's get out out the loop... Not about the number of time your enter a noun !! Hope this will help !