Search code examples
stringkotlinsplittrim

Calculate length of 2 Strings and add them fails


I am having a problem with a string length calculation which I can't solve. So the whole thing is from a book I am working through on kotlin programming: Big Nerd Ranch Guide. There is a tavern menu that should be formatted in code. There is a menu list provided which looks like this:

shandy,Dragon's Breath,5.91
elixir,shirley temple,4.12
meal,goblet of la croix,1.22
desert dessert,pickled camel hump,7.33
elixir,iced boilermaker,11.22

This list should be formatted into something like this:

*** Welcome to Taernyl's Folly ***

Dragon's Breath...............5.91
Shirley Temple................4.12
Goblet Of La Croix............1.22
Pickled Camel Hump............7.33
Iced Boilermaker.............11.22

So I have created a function that takes the menu data and splits it based on the comma (thanks for the correction @gidds). I don't need the first entry of each row. This is the function:

fun createMenuList(menuData: List<String>) {
    // print the Header with the Tavern Name
    println("*** Welcome to $TAVERN_NAME ***\n")

    // List of the parts of the beverage name since I need to capitalize each part of the name
    var nameList: List<String>

    menuData.forEachIndexed { _, menuData ->
        // the menu data is split by comma and and stored in beverageName and beveragePrice
        var (_, beverageName, beveragePrice) = menuData.split(",")

        // next line I calculate the dots I need to fill in the
        // space between the name and the price based on a line length of 34
        val dotCount = 34 - (beverageName.length + beveragePrice.length)

        // if I print the calculations for both Strings the calculation works
        // for the beverageName but not for beveragePrice, it's always 5 even though it is not
        println("Namelength:${beverageName.length} Pricelength:${beveragePrice.length}")

        var counter = 0
        var dots = ""

        // add as many dots as calculated by the string operation for the space
        while (counter <= dotCount) {
            dots += "."
            counter++
        }

        nameList = beverageName.split(' ')
        var cappedName = ""
        for (name in nameList) {
            cappedName += name.replaceFirstChar { it.uppercase() } + " "
        }
        cappedName = cappedName.substring(0 until cappedName.length - 1)
        println("$cappedName$dots$beveragePrice")

    }
}

If I run the code the length of the beveragePrice always gets calculated as 5, even though it should be 4 for the first 4 entries. This results in the wrong line length for the first 4 entries, I don't know what I am doing wrong. Here is the result I get with the printed string lengths. The funny thing is the same code works on a windows machine correctly. I appreciate every help I can get so thanks in advance.

*** Welcome to Taernyl's Folly ***

Namelength:15 Pricelength:5
Dragon's Breath...............5.91
Namelength:14 Pricelength:5
Shirley Temple................4.12
Namelength:18 Pricelength:5
Goblet Of La Croix............1.22
Namelength:18 Pricelength:5
Pickled Camel Hump............7.33
Namelength:16 Pricelength:5
Iced Boilermaker..............11.22

Solution

  • Thanks everyone contributing to the answer of my question as Tenfour04, Henry Twist and gidds in the comments. Tenfour04 gave the initial right answer. Line breaks are getting added to the elements in the list after split. I have seen it on windows now happen as well. So one should always use trim() with split() when you read strings from a file I guess. Solution is:

    var (_, beverageName, beveragePrice) = menuData.trim().split(",")