Coding Bat – mixStart

This exercise requires that the program return the value of true if what has been entered is a 3-letter word with the 2nd and 3rd letters being “ix”.

Here is the code that you start with:

public boolean mixStart(String str) {

As you can see, we have a String variable named “str”.

public boolean mixStart(String str) {
  if (str.length() < 3) return false;
  String two = str.substring(1, 3);
  if (two.equals("ix")) {
    return true;
  } else {
    return false;

In the solution, first check the length of the statement using the length function of the String class. If the length is less than 3 characters long, the program stops running and returns “false”.

Assuming that the value of “str” is at least 3 characters long we move to using the substring function of the String class to create the value to be assigned to variable “two”. The substring starts at index 1 and stops at index 3. This means that the variable “two” will only have the 2nd and 3rd letters of the variable “str”.

We now begin the analysis of the variable “two”. If it is “ix” then the program returns true. However, if it is NOT “ix” then the program returns false.

You will notice that the comparison is being made using the following:

if (two.equals("ix")) {

Some have asked if we could use something similar to the following:

if (two == "ix") {

While this looks like this would work in comparing a string variable to an existing string, we must use the equals function of the String class to determine if two strings are equivalent.

Traditional boolean operators such as == and != do not work when comparing strings.

Leave a Reply

Your email address will not be published. Required fields are marked *