Vowel check - out of bounds error

I am trying to write a program that takes a lowercase word, converts it to uppercase, and changes the vowels in the word to the next alphabet. So far I have done this:

import java.util.*;
class prg11
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a word in lowercase.");
        String word = sc.next();
        word = word.toUpperCase();
        int length = word.length();
        char ch[] = new char[length+1];
        for (int i = 0; i<=length; i++)
        {
            ch[i] = word.charAt(i);
            if("aeiou".indexOf(ch[i]) == 0)
            {
                ch[i]+=1;
            }
        }
        String str = new String(ch);
        System.out.println(str);
    }
}

      

The code compiles fine. But when I run the program and type in a word, say hey, the word is only printed in uppercase. The vowels in it (in this case "e") do not change to the next alphabet. How do I resolve this? TIA.

+3


source to share


5 answers


I think this should do it, let me know if it isn't

public class prg11 {
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter a word.");
    String word = sc.next();
    sc.close();
    word = word.toUpperCase();
    int length = word.length();
    char ch[] = new char[length+1];
    for (int i = 0; i<length; i++) {
        ch[i] = word.charAt(i);
        if("AEIOU".indexOf(ch[i]) > -1) {
            ch[i]+=1;
        }
    }
    String str = new String(ch);
    System.out.println(str);
}
}

      



Let me know if it works.
Happy coding;) -Charlie

+2


source


Three places need to be changed to match the code in the question.

word = word.toUpperCase();
int length = word.length();

// yours: char ch[] = new char[length + 1];
// resulting array needs to be as same length as the original word
// if not, there will be array index out of bound issues
char ch[] = new char[length];

// yours: for (int i = 0; i<=length; i++)
// need to go through valid indexes of the array - 0 to length-1
for (int i = 0; i < length; i++) {
    ch[i] = word.charAt(i);
    // yours: if ("aeiou".indexOf(ch[i]) == 0) {
    // two problems when used like that
    // 1. indexOf() methods are all case-sensitive
    //    since you've uppercased your word, need to use AEIOU
    // 2. indexOf() returns the index of the given character
    //    which would be >= 0 when that character exist inside the string
    //    or -1 if it does not exist
    //    so need to see if the returned value represents any valid index, not just 0
    if ("AEIOU".indexOf(ch[i]) >= 0) {
        ch[i] += 1;
    }
}

      

Here's a short version. Pay attention to the changes I made.



String word = sc.next().toUpperCase();
char ch[] = word.toCharArray();
for (int i = 0; i < ch.length; i++) {
    if ("AEIOU".indexOf(ch[i]) >= 0) {
        ch[i] += 1;
    }
}

      

Java doc indexOf () .

public int indexOf(int ch)

Returns the index within this string of the first occurrence of the specified character.
If a character with value ch occurs in the character sequence represented by this String object,
then the index (in Unicode code units) of the first such occurrence is returned.
For values of ch in the range from 0 to 0xFFFF (inclusive), this is the smallest value k such that:

     this.charAt(k) == ch

is true. For other values of ch, it is the smallest value k such that:

     this.codePointAt(k) == ch

is true. In either case, if no such character occurs in this string, then -1 is returned.

Parameters:
    ch - a character (Unicode code point).
Returns:
    the index of the first occurrence of the character in the character sequence represented by this object,
    or -1 if the character does not occur.

      

+3


source


Using:

for (int i = 0; i<length; i++)

      

instead, since the last index is length-1.

+1


source


use for (int i = 0; i<=length-1; i++)

instead for (int i = 0; i<=length; i++)


and if("AEIOU".indexOf(ch[i]) != -1)

instead ofif("aeiou".indexOf(ch[i]) == 0)

reason
1.array index starts at 0, so length-1


2. Since you already made your string uppercase, check the "AEIOU" condition
3. every unspoken character returns -1, so useif("AEIOU".indexOf(ch[i]) != -1)

+1


source


"aeiou".indexOf(ch[i]) == 0

will only match 'a' characters (since that is the character at index 0). You should search for any index greater than -1. Also, since you have already converted the string to uppercase, you should check for "AEIOU" instead of "aeiou".

+1


source







All Articles