Step 3

Creating the checkString function


Next, copy the code as shown. It should go right below the printStatus() function. Be careful with the ending “}” on line 15. This was moved from line 10, remember this is needed to close the code for the entire “WordChecker” class.

Here we have a new function called “checkString” which will be used to check the String/text given to it. It has a variable of type String called “toCheck” inside the “()”. By doing this, we are creating something called a parameter.

A parameter is a variable/data required by a function to do its job. Here we are saying that in for the “checkString” function to work, it needs a String variable to check. We give this variable the name “toCheck.” (A String is the name of the datatype used for text.)

We then start off our function by adding 1 to “numberChecked” and printing the new status of “WordChecker.” The next bit of code will actually check the String “toCheck.”

The if statement


Next up, add the following code below the code we just added. Here we see a new piece of code called the if statement.

An if statement is a tool programmers use to control code to only happen under certain conditions.

The way it works is you put some sort of question/query inside the () next to the word if. If the answer to the question is True, the code inside the if statement’s body (the code in the {}) will execute. If the answer to the question is False, the code in the body is skipped.

In this example, we check if the String “toCheck” is empty/has no text inside it. If it is, we print out a message stating this, feel free to customize this message.

The else and else if statements


Next, up add the following code below the code we just added. Here we see some more new pieces of code, the else if statement and the else statement.

If the code in an if statement gets skipped, the else if statement acts as another check. You can have as many else if statements as you need, and they behave just like if statements. If their condition is True the code inside it gets executed, otherwise it moves on.

The else statement contains the code that gets executed if all the if/else if statements get skipped. If any of its connected if/else if statements get executed, then the else statement gets skipped.

Here we first check if “toCheck” is less than 10 characters with the if statement. If it is, we print out a message. If it isn’t, we then check if it has more than 25 characters with the else if statement. If it is we print out a different message. If it isn’t, we print out a different message using the else statement.

Lastly, we end the code with a return statement, after all the checks have been made and a print has been completed.