1

I know that simillar questions have been asked, but they all had the same problem: inside of the loop they had something like

i <= aString.lenth()

I used

i < phrase.length();

and I'm still getting the error. I also tried

i < phrase.length()-1;

Any ideas what is wrong?

Thanks.

public class WordPlay {
    public boolean isVowel(char c) {
        if(c=='a' || c=='A' || c=='e' || c=='E' || c=='i' || c=='I' || c=='o' || c=='O' ||     c=='u' || c=='U') {    
            return true;
        }    
        else
        {
            return false;
        }    
    }
    public void testIsVowel () {
        System.out.println(isVowel('F'));
    }
    public String replaceVowels (String phrase, char ch){
        StringBuilder replaced = new StringBuilder(phrase);
        for (int i = 0; i<phrase.length(); i++) {
            char currChar = phrase.charAt(i);
            if (isVowel(currChar)){

                    //the line below causes the error
                    replaced.setCharAt(currChar, ch);
            }    
        }    
        return replaced.toString();    
    }
    public void testReplaceVowels() {
        System.out.println(replaceVowels("Hello World", '*'));

    }    
}
5
  • @JohnSmith the error is in the title - java.lang.StringIndexOutOfBoundsException: String index out of range: Commented May 26, 2016 at 10:59
  • Which line is giving you the error? Please edit your question and include the relevant part of the stack trace. Commented May 26, 2016 at 10:59
  • 1
    At wich iteration does it happen? Commented May 26, 2016 at 11:00
  • @LutzHorn it is included - there is a comment above it. Commented May 26, 2016 at 11:00
  • 1
    That is the error message. But it must include the line number where it happens. Which line is causing the error? Commented May 26, 2016 at 11:04

2 Answers 2

6

In your call to StringBuilder.setCharAt

replaced.setCharAt(currChar, ch);

the first argument should be i, not currChar:

replaced.setCharAt(i, ch);

You should pass the index you want to set the char at, not the character itself.

currChar's int value is probably higher than the length of your StringBuilder, which caused the exception, but if it wasn't, you would get strange output without an exception, which is even worse.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much, it makes sense now.
2

The following code is the setCharAt method of AbstractStringBuilder class.

public void setCharAt(int index, char ch) {
    if ((index < 0) || (index >= count))
        throw new StringIndexOutOfBoundsException(index);
    value[index] = ch;
}

If you are passing character 'a' to setCharAt method, it will be implicitly cast from character to int value 97 at index . It made java.lang.StringIndexOutOfBoundsException. Thanks :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.