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

Michael Werlinger
Michael Werlinger
1,259 Points

Taking boolean do while statements to the next level

Hey All!!
So this video really got my brain spinning, if there is a class further along that explains this please tell me where so I can work in that direction but in the mean time. So this lesson we learned about do while statements and as programmers we do not want to repeat our work.

      String noun;
      boolean isInvalidWord;
      do {
          noun = console.readLine("Please select a noun: ");
          isInvalidWord = (noun.equalsIgnoreCase("dork") || 
                                   noun.equalsIgnoreCase("jerk"));  
          if (isInvalidWord) {
          console.printf("That language is not allowed.   try again... \n\n");
          }
      } while(isInvalidWord);

So how do we take this to the next level so we can say and please excuse my lack of elegance here. I don't even know if this is possible so I will illustrate this from my brain the best way I can.

Restricted = boolean restrictedWordList = (%.equalsIgnoreCase("dork") ||
                                                   %.equalsIgnoreCase("jerk");
                     if(restrictedWordList){
                     console.printf("That language is not allowed. Please try again... \n\,");
                     }

String noun;
do {
      noun = console.readline("Please select a noun: ");
      (restrictedWordList,noun);
      }while(restrictedWordList);  

Anything to lead me in the direction my brain is running if this is even possible would be greatly appreciated! I know it might be an advanced concept but please tell me where it is if it is in the course so I can look forward and work twords it or please explain how this is done. This has applications in tons of places in code we write so this would be crazy useful to know more about!

Yanuar Prakoso
Yanuar Prakoso
15,196 Points

First of all please forgive my lack of understanding on what do you mean by taking it to the next level. For the principle of Do While Loop I guess as long as you follow the Java track in Treehouse you will learn a lot and gain much experience that you will be use it in your own account as you will get use to it,

If you need another resources just google it and I am sure many forums or websites will give you plenty of insights and examples on Do While Loop subject. Maybe the Oracle Java Doc on Do While Loop will also a good place to start.

As for your code that you said the illustration from what you were thinking I find it hard to understand. Thus I will just give you some pointer regarding the code. First of all when you are dealing with loop (for loop, while loop, do while loop) in any programming language always remember one important rule: Make sure your code is converging towards limiting value. This is to avoid what is called infinite loop.

For instance in the original code from the course:

String noun;
      boolean isInvalidWord;
      do {
          noun = console.readLine("Please select a noun: ");
          isInvalidWord = (noun.equalsIgnoreCase("dork") || 
                                   noun.equalsIgnoreCase("jerk"));  //<- here the users is given a chance to change the isInvalidWord
                                                                                         // from true to false (converging)
          if (isInvalidWord) {
          console.printf("That language is not allowed.   try again... \n\n");
          }
      } while(isInvalidWord);//<- this will keep looping as long as isInvalidWord is true!

I put some notes in it. As you can see the do While loop will keep on looping as long as isInvalidWord is True. Thus it needs to converge to a case where the isInvalidWord is equal False. This condition can be achieved hence the code inside the loop gives an opportunity for the user to enter a noun that is can change the value of isInvalidWord to False (I gave a note in the code about this as well).

If you have the chance to visit Oracle Java Doc on Do While Loop you will see another example of how to modify a variable (in this case by incrementing the variable using ++) inside do while loop into converging towards limiting value inside while statement.

As for your code:

Restricted = boolean restrictedWordList = (%.equalsIgnoreCase("dork") ||
                                                   %.equalsIgnoreCase("jerk");
                     if(restrictedWordList){
                     console.printf("That language is not allowed. Please try again... \n\,");
                     }

String noun;
do {
      noun = console.readline("Please select a noun: ");
      (restrictedWordList,noun);
      }while(restrictedWordList);  

I will set aside any syntax problem in your code and focus only on how you built the do while loop. What is lacking in you do while loop is you fail to give a code that ensure or give a chance to make restrictedWordList become False in value. You did not specify or at least give reference or call a method to process where it can change the value of restrictedWordList becomes True or False.

Again as I said I am having a hard time understanding what do you mean by taking this to the next level. Since do while loop is common practice in many programming language it is best to practice it as much as possible to gain deep understanding on the subject.

1 Answer

It looks like you want to create another method that does the work of checking the input word against the list of restricted words.

If Java is your first programming language, I'm very impressed at your instinct. That is indeed the correct thing to do. And yes, future Java courses will expand on how to organize your code in methods and classes, so they can be reused in different contexts. For now you're doing everything inside the main method, but don't worry. It won't last.

Once you're finished with Java Basics, do Java Objects. Pretty soon you'll be able to answer your own question. Have fun!