Kotlinlearncs.online LogoJava
Return to List

Solve: Reformat Name and Age Records

Imported By: Geoffrey Challen
/ Version: 2022.7.0

Sometimes we need to transform data from one format to another. In this problem we'll start with CSV-formatted records containing names and ages, and transform them to a more user-friendly format.

We'll start with an input CSV String in the following format:

Gracie,1
 Banjo, 12
Lulu, 7

And we want to return a String in the following format:

Gracie is 1 year old
Banjo is 12 years old
Lulu is 7 years old

Note how the first item has year singular, but the others do not.

Write a method reformatRecords that accepts a non-null String containing CSV in the format described, and returns a String in the "is ... years old" format shown above. You can assume that the input String contains at least one record, and that each line has a comma-separated name and age pair. However, as shown above there may be whitespace surrounding the values in each record, so you will need to use trim appropriately.

Related Lessons

Stuck? You may find these lessons helpful: