Home > OS >  How can you capitalize first letter of a string in java when using StringBuffer
How can you capitalize first letter of a string in java when using StringBuffer

Time:10-05

i have some issues when wanting to capitalize the first letter of a StringBuffer, so my problem basically says "capitalize each word from a string" and the main function is

public class Main {
    public static void main(String[] args) {
        StringBuffer title = new StringBuffer("this text will be capitalized");
        Translator.transformaCuMajuscule(title);
        System.out.println(title); // This Text Will Be Capitalized
    }
}

and also, in the enunciation it is mandatory to use my method with StringBuffer as it follows "public static void capitalize(StringBuffer words)"

and my complete code is:

class Translator {
    private StringBuffer title = new StringBuffer();
    public Translator(StringBuffer title) {
        this.title = title;
    }
    public static boolean isLetter(char c) {
        if(Character.isLetter(c)) {
            return true;
        }
        return false;
    }
    public static void lowerToUpper(char c) {
        c = Character.toUpperCase(c);
    }
    public static void Capitalize(StringBuffer words) {
        for (int i = 1; i < words.length(); i  ) {
            if (words.charAt(i) == ' ' && isLetter(words.charAt(i   1))) {
                words.setCharAt(i   1, Character.toUpperCase(words.charAt(i   1)));
            }
        }
        String cuvinte = title.substring(0,1).toUpperCase(); // here is the issue, basically at the moment my program outputs this Text Will Be Capitalized so i need to know how to make the first letter of the string upper case and that's it.
    }
}
public class Main {
    public static void main(String[] args) {
        StringBuffer titlu = new StringBuffer("this text will be capitalized");
        Translator.Capitalize(titlu);
        System.out.println(titlu); // this Text Will Be Capitalized - my output  "This Text Will Be Capitalized - good output
    }
}

CodePudding user response:

In your version you check for spaces and capitalize the character after a space. The very first character `words.charAt(0) needs to be capitalized as well. A simple (slightly unclean) solution would be something like this:

words.setCharAt(0, Character.toUpperCase(words.charAt(0)));
for (...

CodePudding user response:

String cuvinte = title.substring(0,1).toUpperCase(); won't work because static void Capitalize(StringBuffer words) is a static method which cannot access the instance variable title - nor should it.

Instead you need to work on words only and you're almost done. Your problem is that the loop starts at the 2nd character and misses some checks.

Instead, use something like this:

for (int i = 0; i < words.length(); i  ) {
   char current = words.charAt(i);

   if( (i == 0 || words.charAt(i - 1) == ' ') && isLetter(current) {
      words.setCharAt(i, Character.toUpperCase(current ));
   }
}

What got changed? Instead of changing the next character you're changing the current character if the following conditions are satisfied:

  • it is the first character or preceded by a space
  • it is a letter

CodePudding user response:

StringBuffer buf = new StringBuffer("test value");

if (buf.charAt(0) >= 'a' && buf.charAt(0) <= 'z') {
    buf.setCharAt(0, Character.toUpperCase(buf.charAt(0)));
}
System.out.println(buf);

CodePudding user response:

You need to handle the capitalization of the first letter too.

public static void Capitalize(StringBuffer words) {
    boolean prevSpace = true;
    for (int i = 0; i < words.length(); i  ) {
        char c = words.charAt(i);
        if (isLetter(c) && prevSpace) words.setCharAt(i, Character.toUpperCase(c));
        prevSpace = Character.isWhitespace(c);
    }
}
  • Related