1

The assignment is to, for example, remove char 'a' from the String "I am Sam am I Sam", this is a bit code I have so far:

public String removeLetters() {
    String cleaned=sentence;
    StringBuilder builder = new StringBuilder(cleaned);

    while(cleaned.indexOf(lookFor) != -1) {
        builder.deleteCharAt(cleaned.indexOf(lookFor));
    }
    return builder.toString();
}

This method returns fine when there's no while loop (though it only removes one char), but when I run it with the while loop I get the OutOfBounds error.

5 Answers 5

1

Change this:

while(cleaned.indexOf(lookFor) != -1) {
    builder.deleteCharAt(cleaned.indexOf(lookFor));
}

with this:

while(builder.indexOf(Character.toString(lookFor)) != -1) {
    builder.deleteCharAt(builder.indexOf(Character.toString(lookFor)));
}
Sign up to request clarification or add additional context in comments.

2 Comments

That did the trick! Thanks for that, now onto trying to understand why, but that's for another day. Thanks again! Edit: turns out I just didn't know what the Character class was.
You must have already done that, but anyway just be sure to notice that I changed your cleaned references for builder, as other members mentioned. Character.toString() is just an utility method for converting a character (which is considered a numeric value in Java) into a string.
1

You can directly do this using String#replace method:

String cleaned = "I am Sam am I Sam";
cleaned = cleaned.replace("a", "");
System.out.println(cleaned);   // I m Sm m I Sm

4 Comments

I think you should use replaceall instead of replace.OP wants to change the all of a
@MelihAltıntaş replace replaces all the occurrences only. replaceAll is for regex replacement. That's the only difference.
Thanks, sorry for my wrong knowledge :) I have already upvoted :)
@MelihAltıntaş No issues. :)
0

The condition in the while loop is the index in the cleaned StringBuilder, but then you're using the same index to delete characters from builder - after the first character has been deleted, they no longer match.

In short:

public String removeLetters() {
    String cleaned=sentence;
    StringBuilder builder = new StringBuilder(cleaned);

    while(builder.indexOf(lookFor) != -1) {
        builder.deleteCharAt(builder.indexOf(lookFor));
    }

    return builder.toString();
}

Comments

0

try this one.

cleaned.replaceAll(lookFor,"");

Comments

0

Have to remove from the builder, not the source string. Also, you initially had an infinite loop, attempting to modify the builder string, but only checking the source string, which wouldn't change. This compiled and ran fine:

public String removeLetters()
{
    String lookFor = "a";
    String original="I am Sam am I Sam";
    StringBuilder builder = new StringBuilder(original);
    while(builder.indexOf(lookFor) != -1) {
        builder.deleteCharAt(builder.indexOf(lookFor));
    }
    return builder.toString();
}

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.