-3
public static boolean AllVowels(String hello) { 
    int numLength = hello.length();
    boolean tValue = true;
    int i = 0;
    while (i<=numLength) {
        char temp = hello.charAt(i);
            if (temp == 'a') {
                tValue = true;
            }
            else if (temp== 'e') {
                tValue = true;
            }
            else if (temp == 'i') {
                tValue = true;
            }
            else if (temp == 'o') {
                tValue = true;
            }
            else if (temp == 'u') {
                tValue = true;
            }
            else {
                tValue = false;
            }
            i+=1;
    }
            return tValue; 
}

I wanted to create a method that, when called, will test the inputted string's contents and would return true or false depending on it contains all vowels. (true if all vowels. false if it contains a single consonant.) I am wondering how could I change this so that as soon as it encounters a consonant, it will immediately change the tValue to false and not test the other letters.

I would appreciate any guidance concerning my error.

M. Ali
  • 15
  • 5
  • Check the `charAt` api docs on input range: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#charAt(int) – tsolakp Nov 07 '18 at 17:55
  • You are asking to access an element outside of the index count. Don't forget arrays count at 0. `<=` conditionals won't work, since they will return a value greater than what the index accepts. – Frontear Nov 07 '18 at 18:01
  • This is the sort of error which should be faster to fix by stepping through your code with your debugger. – Peter Lawrey Nov 07 '18 at 18:36

1 Answers1

1

You are going all the way to the length. You must change i<=numLength to i < numLength

Michael Curtis
  • 564
  • 3
  • 9