Search code examples
javapunctuationcapitalize

Java. Capitalizing, adding period


Working on this rookie task and need help.

This code must: Capitalize the first letter of the first word. Add a period (.) to the end of the sentence. Join the words into a complete string, with spaces. Do no other manipulation on the words.

Here is an example of what this code should do: INPUT "i", "am", "super", "HERO" OUTPUT I am super HERO.

I thought about this in that case:

import java.util.StringJoiner;
import java.lang.String;

public class My {
    public static void main(String[] args) {

        String[] arr = new String[]{"i", "am", "super", "HERO"};
        StringJoiner sj = new StringJoiner(" ");
        //StringBuffer sb = new StringBuffer(".");
        for (String s : arr) {
            sj.add(s);
        }
        sj.append(".");
        sj.capitalize();
        System.out.println(sj);

}

But catching error at "append" and "capitalize" - cannot resolve method. Pls, help.


Solution

  • Explanation

    A StringJoiner does not offer methods append and capitalize, thus you get the error. Check the documentation.

    The . should be added on the result of sj.toString(), i.e. String result = sj.toString() + ".";. Capitalization must be done on the first word s, not on the StringJoiner.


    Solution

    Here is a working snippet putting all together:

    String[] arr = new String[]{"i", "am", "super", "HERO"};
    
    StringJoiner sj = new StringJoiner(" ");
    // Capitalize first word
    sj.add(Character.toUpperCase(arr[0].charAt(0)) + arr[0].substring(1));
    // Add the remaining words
    for (int i = 1; i < arr.length; i++) {
        sj.add(arr[i]);
    }
    String result = sj.toString() + ".";
    
    System.out.println(result);
    

    Note that you may want to check for the edge case where there is no word in arr at all, i.e. where it is empty. The current code would then fail.


    Do yourself a favor and pack the capitalize code into a small utility method:

    private static String capitalize(String word) {
        return Character.toUpperCase(word.charAt(0)) + word.substring(1);
    }
    

    Then the code is just:

    String[] arr = new String[]{"i", "am", "super", "HERO"};
    
    StringJoiner sj = new StringJoiner(" ");
    // Capitalize first word
    sj.add(capitalize(arr[0]));
    // Add the remaining words
    for (int i = 1; i < arr.length; i++) {
        sj.add(arr[i]);
    }
    String result = sj.toString() + ".";
    
    System.out.println(result);
    

    Stream Solution

    In case you like the Stream-API:

    String result = capitalize(arr[0])
        + Arrays.stream(arr).skip(1)
            .collect(Collectors.joining(" "))
        + ".";
    

    StringJoiner Suffix

    You can also do the addition of "." by using StringJoiners suffix:

    StringJoiner sj = new StringJoiner(" ", "", ".");
    

    It will then automatically add the "." to the end, when you do toString.

    Similar for the Stream solution:

    Collectors.joining(" ", "", ".")