fun printRoom() {
println("Cinema: ")
val rows = 7
val columns = 8
val seats = CharArray(columns) { 'S' }.joinToString { " " }
for (i in 1..rows) {
println("$i" + " " + seats)
}
}
any help will be appreciated. Im sure this is something simple im doing but I can't figure out why this keeps printing commas instead of S's
CharArray.joinToStringhas these parameters, all of which are optional and have default values:joinToStringallows you to use your own separator, add your own prefix and postfix, have an optional custom limit on the joining, and have a custom string for when that limit is reached, and also optionally transform theChars into some otherStringfirst, before joining them together.By passing in
{ " " }, you pass a lambda expression that simply returns the string" ". This corresponds to thetransformparameter. Kotlin thinks that you want to transform everyCharto the string" "first, and then join the" "together! Because you didn’t pass theseparatorparameter, the default value of”, “is used as the separator, which is why you see a lot of commas.What you intended on doing is passing
" "as theseparatorparameter. Don't write a lambda:You can also be very explicit about this and say:
If you don't mind a trailing space,
repeatalso works: