Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The output for below code is:
CSS
Number of vowels: 2
Number of vowels: 3
Number of vowels: 2
Number of vowels: 0
Number of vowels: 6


However, if I change the '>=' to '>' for line:
if (VOWELS.indexOf(word.charAt(i)) >= 0)


the output will become:
CSS
Number of vowels: 2
Number of vowels: 1
Number of vowels: 1
Number of vowels: 0
Number of vowels: 6


I believe using '>' is the correct way, but the actual output does not produce the right answer. My question is why '>=' produce the correct answer but not '>'?



C#
public class MyProgram
{
    public void start()
    {
        printNumVowels("rumpus");
        printNumVowels("Squabash");
        printNumVowels("Flippant");
        printNumVowels("hymn");
        printNumVowels("MISDEMEANOUR");
    }

    private void printNumVowels(String word)
    {
        final String VOWELS = "aeiouAEIOU";
        int numVowels = 0;
        int lenWord = word.length();

        for (int i = numVowels; i < lenWord; i++)
        {
            if (VOWELS.indexOf(word.charAt(i)) >= 0)
            {
                numVowels++;
            }
        }

        System.out.println("Number of vowels: " + numVowels);
    }
}
Posted

When we count things, we start from one, right? That is not what computer programs do, they start from zero. So indices start from 0, which we mortal always think of 1.
To interpret that line of code that you mentioned, we start from the inner then move outward, like this:
1. (word.charAt(i)) gets a character at the index position i (where the character starts from zero, remember) from the word string. Let's say it is 'e'. Now move one step outward.
2. VOWELS.indexOf("e") means to find a "e" inside the VOWELS string and returns its index position (again, it starts from zero), if not found, it returns -1.
3. That explains why using >=0 is correct, because 0 means the 'e' is found at the first position.
Read more: String.IndexOf Method[^]
 
Share this answer
 
v2
In in this case, the correct operator to use would be '>='.

If you debug your code, you should see that the 'a' characters are the ones not picked up if you use '>'.

The reason for that is that Java uses zero based indexing, and as String.indexOf will return the index of the character in the string, first character will be at index 0. If the character can not be found in the string, indexOf will return -1.

So, if you really want to use the '.' operator, change the check to

Java
if (VOWELS.indexOf(word.charAt(i)) > -1)
{
    ...
}
 
Share this answer
 
For the second word Squabash look for the 4th letter a. If you see the position of this letter, a position is first in VOWELS.

For this letter [a] VOWELS.indexOf(word.charAt(i)) will return 0 and your check > 0 will fail hence will not count.

Hope this makes you understand.
 
Share this answer
 
To answer your question, .indexOf will return 0 or greater for any match and -1 for no match. The index value returned is zero based, so if you, for example, test 'ark'.indexOf('a') > 0, the answer will always be false because the return will be 0 (the first character in the string).

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(int)

If you changed your if test to read:
if(VOWELS.indexOf(word.charAt(i)) != -1)
{
    numVowels++;
}

it would make more sense.

Another change I would make is your vowels string, make it just uppercase (I've read that uppercase is best to test against but could be wrong) and then convert the string you are testing to uppercase before testing. This would cut down on the total loops (if your worried about that). As shown below, you could use a temporary string (_toTest) if you need to use the input string in your print line at some point.

private void printNumVowels(String word)
{
    final String VOWELS = "AEIOU";
    string _toTest = word.toUpperCase();
    int numVowels = 0;
 
    for (int i = 0; i < _toTest.length(); i++)
    {
        if (VOWELS.indexOf(_toTest.charAt(i)) != 0)
        {
            numVowels++;
        }
    }
    System.out.println("Number of vowels: " + numVowels);
}


Another thing that bugged me is when initializing your for loop, you used numVowels to initialize i, it may not hurt anything but to me it's just something I wouldn't do unless you intend on dynamically changing the start position of your loop.

I haven't tested this code, I typed it out on my tablet and it's 5am so just be warned! =)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900