-1

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.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
upandgreen
  • 69
  • 5
  • A `StringJoiner` does not offer methods `append` and `capitalize`, thus you get the error. Check the documentation. – Zabuzard Feb 11 '19 at 13:28
  • 1
    `StringJoiner` is a good choice, due to the spaces he needs to add. 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`. – Zabuzard Feb 11 '19 at 13:31
  • "catching error" isn't a problem description people can easily help with ... – GhostCat Feb 11 '19 at 13:40

3 Answers3

3

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(" ", "", ".")
Zabuzard
  • 25,064
  • 8
  • 58
  • 82
  • Nice would be to mention `new StringJoiner(" ", "", ".")` but you already paid the effort of a stream solution. – Joop Eggen Feb 11 '19 at 13:44
0

A little example with the StringBuilder that you can check here https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html, that have the append method.

For word capitalize you can check How to capitalize the first character of each word in a string

Example

    String[] arr = new String[]{"i", "am", "super", "HERO"};
    StringBuilder sb = new StringBuilder();

    for (String word : arr){
        sb.append(WordUtils.capitalize(word));
    }

    sb.toString();
Tlaloc-ES
  • 4,825
  • 7
  • 38
  • 84
0

There is no way for doing it using a StringJoiner.

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

    String[] arr = new String[]{"i", "am", "super", "HERO"};
    arr[0].toUpperCase();

    StringJoiner sj = new StringJoiner(" ");

    for (String s : arr) {

        sj.add(s);
    }
    sj.append(".");

    System.out.println(sj);
}
sali333
  • 125
  • 4
  • 15
  • "*There is no way for doing it using a StringJoiner.*" - but you just did it with `StringJoiner`, I am kind of confused. – Zabuzard Feb 11 '19 at 13:42
  • 1
    Note that `sj.append(".")` will result in space between the last word and the full stop. I.e. `"foo ."` instead of `"foo."` – Zabuzard Feb 11 '19 at 13:43