Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Help to check my code. Convert every first word capital letter

public class test {
    public static void main(String[] args) {
        /**
         * Convert every first word to capital letter like hello world to Hello World
         **/

        start();
    }

    public static void start() {

        String hej = "hello world";
        String[] x = hej.split(" "); // ["hello", "world"]

        something(x);
    }

    public static String something(String[] x) {

        String d = ""; //TODO: here is the problem!
        for (int n = 0; n < x.length; n++) {
            d = x[n].toUpperCase().charAt(0) + x[n].substring(1);

        }// end outer loop
        return onTheWay(d); //TODO: It returns only the last word!
    }

    public static String onTheWay(String old) {
        //TODO: Not finished yet..
        String alpha = "abcdefghijklmnopqrstuvxyz";
        String convertedAlpha = alpha.toUpperCase(); 
        String r = "";

        for (int y = 0; y < convertedAlpha.length(); y++) {
            if (old.indexOf(0) == convertedAlpha.indexOf(y)) {
                r = old.concat(" ");
            }


        }//end inner for
        return result(r);
    }

    public static String result(String rOld) {
        System.out.print(rOld);
        return null;
    }


}//end class

The first problem is in the method something(...) When "hello" converted to "Hello" then it goes to global variable d and dissapared. I dont know how to solve it out to save result. I tried to move String d inside the for loop and the return too but it gave me the last word (=__=)

Ronald Williams Thank you so much!!! Very clear explanation! Now I understand who d += do! :) I also removed other "junk" methods. All clean and clear! Thanks a lot!

1 Answer

public static String something(String[] x) {

        String d = ""; //TODO: here is the problem!
        for (int n = 0; n < x.length; n++) {
            d += x[n].toUpperCase().charAt(0) + x[n].substring(1) + " ";

        }// end outer loop
        return onTheWay(d); //TODO: It returns only the last word!
    }

In your something method, change the line in your for loop from d = to d +=. With just d = you are only storing the last entry of the for loop. Also at the end of the line in your for loop I add + " " so that it adds a space in there.

For example your method on the first run is saying d = Hello. Then on the second run it is saying actually, scratch that make d = World.

When you change it to d += on the first run it says d = Hello. Then on the second run d = d + World. The += means taker what d already equals is plus what you are trying to add.

Converted to an answer - Dane E. Parchment Jr. (Moderator)