String.length calculation not working properly

Hello community,

I am having some trouble with IntelliJ and kotlin on Ubuntu 18.04. I am trying to calculate some string lengths for configuring entries in a menu (yes this is from the book "Kotlin Programming" from big nerd ranch). I've created a function that takes a menu List which formats the text to a more pleasent menu. Here is the code:

fun createMenuList (menuData: List<String>) {
println("*** Welcome to $TAVERN_NAME ***\n")
var nameList: List<String>

menuData.forEachIndexed { index, menuData ->

val (_, beverageName, beveragePrice) = menuData.split(',')
val dotCount = 34 - (beverageName.length + beveragePrice.length)
var counter = 0
var dots = ""

while (counter <= dotCount) {
dots += "."
counter++
}

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

}
}

The menu data I give to the function 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

So the 1st part is ignored and the 2nd part gets assigned to beverageName and the 3rd part gets assigned to beveragePrice. I am calculating the amount of dots I have to fill in the line so the menu entries all have the same width as the "welcome message".
The problem is that the calculation for dotCount fails for the last line, so something is not right. Here is the result on the linux machine:

If I print the string length's for beverageName and beveragePrice  the problem is obvious:

println("${beverageName.length} ${beveragePrice.length}")

Somehow the price is counted incorrectly with my code which results in the last line being too long.

I tried the same code on windows 10 machine and there it calculates the string values correctly. I have no idea what is going on since the calculation for the string length of the beverageName is calculated correctly. It is some very weird behaviour and since I am pretty new to the whole programming thing I am not sure if I am making a mistake or if this is a bug.

Need some help with this...

 

 

请先登录再写评论。