0

I am trying to write a method to capitalize the first word of every element in an arraylist. So if the arraylist is:

"mary had a little lamb"

It would need to change every element to:

"Mary Had A Little Lamb"

This is the code, and this is the only way I can get it to compile. Every other way gives me an error that says that it can't convert an object to a String.

public static void capitalize(List<String> words)
{
    String str = "";
    ListIterator iter = words.listIterator();
    while (iter.hasNext())
    {
        str = iter.next() + "";
        str.toUpperCase();
        iter.set();
    }
}

Hope that was clear enough. Thanks

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
KyleAK
  • 1
  • 3

1 Answers1

3

Since strings objects dont mutate this is not working:

str.toUpperCase();

the changes are getting done but are getting lost too

you have to do:

str = str.toUpperCase();
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97