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

atolagbe elijah
atolagbe elijah
Front End Web Development Techdegree Student 464 Points

please why was a boolean used in the program is here any use,i dont really get it

i dont really get the boolean aspect

2 Answers

Okay, so a boolean is a data type just like String or Int, but a boolean has either a value of true or false.

In the first part of the video Craig is running a loop that will do something as long a certain condition evaluates to true:

do {
    // Ignoring the code here
} while (noun.equalsIgnoreCase("dork") || noun.equalsIgnoreCase("jerk"))

As long as the string that is stored in the variable called noun is either "dork" or "jerk" the loop will continue to run.

Later in the video he extracts out this expression:

noun.equalsIgnoreCase("dork") || noun.equalsIgnoreCase("jerk")

into a boolean variable called isInvalidWord which will store either true or false depending on whether or not the variable noun equals either "dork" or "jerk". He does this because he originally had that expression:

noun.equalsIgnoreCase("dork") || noun.equalsIgnoreCase("jerk")

twice in his code (at the 4:00 mark in the video it is on lines 26/27 as well as line 30) and duplicating the same code multiple times is bad practice so extracting it into its own variable solves this duplication.