hello
hello

Reputation: 77

Replacing characters

I have a String entered by the User. I'm trying to replace all non-uppercase letters(i.e. numbers, or symbols) with spaces without using methods such as replace(), Stringbuilders, or arrays.

This is what I have so far :

public static void formatPlaintext(String sentence){

  String sentenceUpper = sentence.toUpperCase();
  System.out.println(sentenceUpper);

  String emptyString = " ";
  for(int i = 0; i< sentenceUpper.length() ; i++){
      char ch = sentenceUpper.charAt(i);
      if(ch < 'A' || ch > 'Z'){
        ch = emptyString.charAt(0);
      }

    }

}//end of formatPlaintext 

I keep getting the error that String index is out of range. I believe it has to do with :

ch = emptyString.charAt(0); 

because emptyString doesn't have any characters. But even if I put an arbitrary constant in, it doesn't replace the non-letters with this arbitrary constant.

Upvotes: 0

Views: 72

Answers (2)

furkle
furkle

Reputation: 5059

This isn't how you replace characters in a Java string. In Java, strings are immutable, so you can't set any given index. Additionally, the charAt() method doesn't and can't do anything to the string you're calling it on - all it does is just return the char at that position. Lastly, you probably shouldn't be using void - return the String with characters replaced at the end of the method, then use the return value. You can accomplish this by iterating through your initial string and build a new string, using the static isUpperCase method of the Character class:

public static String formatPlainText(String sentence) 
{
    String replacedSentence = "";

    for(int i = 0; i< sentence.length() ; i++){
        char ch = sentence.charAt(i);

        if (Character.isUpperCase(ch)) {
            replacedSentence += ch;
        }
        else {
            replacedSentence += " ";
        }
    }   

    return replacedSentence;
}

If you're going to be using this frequently, or for particularly long Strings, you should absolutely use a StringBuilder instead - concatenating String on a character-by-character basis is deeply inefficient.

Upvotes: 3

jbarrueta
jbarrueta

Reputation: 5175

You have to remember that arguments in Java are passed as values, not as references, and in this case the String is an immutable object, i.e. an String cannot be changed, when you do a concatenation or replace you're effectively creating a new String.

What I would recommend is to modify your code a little bit to return the new String that was built.

public static String formatPlaintext(String sentence){

  String sentenceUpper = sentence.toUpperCase();
  System.out.println(sentenceUpper);

  StringBuilder builder = new StringBuilder;
  for(int i = 0; i< sentenceUpper.length() ; i++){
      char ch = sentenceUpper.charAt(i);
      if(ch < 'A' || ch > 'Z'){

       builder.append(' ');

      } else {
         builder.append(ch);  
      }
  }
  return builder.toString();
}

Upvotes: 0

Related Questions