Craig Mattson (Personal Website)
Home - Blog, News, About MePrograms - C#.Net, Java, VB6MusicWebsites

Viewing News Article

Using methods within methods and String manipulation (09/06/2007 12:40:06 AM)

For those of you who are studying Java or Object Orientation Programming at University, here is something that may be useful to you. Basically this question came up in sample exam, and I was appauled to see the answer completely incorrect in terms of the lack of depth within the answer and the approach that when compiled, produced the wrong results. Here is the question for those who don't know, and the proper solution for it.

Question: Write a method called soundex to convert a string to a soundex code using the following criterion

  • All characters must be in UPPER CASE
  • Leave the first character in
  • Replace double letters with single
  • Omit the following characters: A, E, I, O, U, W, H, Y
  • and only produce a maximum of 4 characters

This question will no doubt be in the exam (or similar atleast) and even for those not doing the course, it is worth a read to see that it is possible to use mutator methods within methods. Here is my version of the proper, fully commented solutions to the question.

To complete this question, you must be familiar with the following methods;
String.charAt(integer); returns a char for the position requested.
String.toUpperCase(); returns all characters to upper case.
String.indexOf(character); returns an int as per the position in the string (-1 if doesn’t exist).

The code for the solution is below:

public String soundex(String aWord);
{
    //Check if there is a value within aWord
    if(aWord == "" || aWord == NULL)
    {
        //Return an empty string
        return "";
    }

    //Create a message variable to return, and a variable containing chars to eliminate
    String message;
    String toElim = “AEIOUWHY”;

    //Complete condition 1 to convert to lower/upper case
    aWord = aWord.toUpperCase();

    //Complete condition 2 to leave the first letter the same
    message = aWord.charAt(0);

    //Step through each individual character
    for(int i=1; i<aWord.length() && aWord.length() > 1; i++)
    {
        //Complete condition 3 to see if the 2 characters are identical
        if(aWord.charAt(i) == aWord.charAt(i-1))
        {
            //break out of the if...else condition (we don’t want identical letters)
            continue;

        //Complete condition 4 to see if the character appears in toElim
        } else if(toElim.indexOf(aWord.charAt(i)) > -1) {

            //break out of the if...else condition (we don’t want eliminated characters)
            continue;

        } else {

            //Add the letter to the string to return
            message += aWord.charAt(i);

            //Complete condition 5 in checking the length is 4
            if(message.length() == 4)
            {
                //If it does, then return the message
                return message;
            }
        }
    }
    
    //Once the loop has exhausted, return what has currently been set
    return message;

}

You should already be familiar with the commands expressed above, but what you may not be familiar is using methods that return a variable or value within methods.

Let’s assume the following:

String word = “TEST”;

We have the following commands and outputs:

word.charAt(2); //This would return the character ‘S’ as it is in position 2 in the string word.
word.charAt(3); //This would return the character ‘T’ as it is in position 3 in the string word.
word.indexOf(‘E’); //This would return the integer 1 as it is the position of the character ‘E’
word.indexOf(‘F’); //This would return -1 as the letter F does not appear in the string.

You should be familiar with this using integers and characters. One thing you should have learned was the fact that you can also use variables in place of the characters and integers, and thus the following could work:

int pos = 2;
word.charAt(pos); //This would return the character ‘S’ as pos = an integer of 2
char ch = ‘E’;
word.indexOf(ch); //This would return the integer 1 as ch = the character of ‘E’

As you know how to do these, there should be little to worry about dynamic variables such as those in a for loop:

for(int i=0; i<word.length(); i++) //This iterates through the individual letters
{
    word.charAt(i); //This will return what the value is at i.
}

So, you know you can get integer variables of datatype int and char to work in indexOf and charAt respectively, but can we do it with methods that return a value (those being accessor methods)? The answer is yes! So, if we have a look at Question 4.1, we created an alphabet of characters to eliminate. Using the combination of indexOf (which is useful for finding out if a character does exist) and charAt (which is useful for getting a character at a given position (or index) in a string.

Lets step back a bit – we said that indexOf can take the value of a character, and the method charAt returns a character. Therefore, is it possible that finding the index of a character within the alphabet will yield the response we wish to obtain? Again, the answer is yes! charAt returns a ‘char’ which is perfectly acceptable to use within String.indexOf(char). This is where something like the following comes in:

toElim.indexOf(aWord.charAt(2)); //In this case would be the equivalent to toElim(indexOf(‘S’));

Using the word TEST, this will mean that aWord.charAt(2) is the same as ‘S’. Therefore, the indexOf(‘S’) becomes -1 as toElim does not contain the letter S. aWord.charAt(1) on the other hand is the same as ‘E’ which is in the toElim string. indexOf(‘E’) returns the integer 1 which states it is in the first position in the string. By manipulating this theory, we can create a conditional statement to complete criterion 4.

if(toElim.indexOf(aWord.charAt(1)) > -1)
{
    continue;
}

The rest of the code is pretty self explanatory (in terms of position = position – 1 for checking the character before the current character), and checking if there have been 4 characters already.

[Print View]