Question: "Challenge: Configureable Status Format"



For this challenge are we supposed to use info we learned in the chapter, i.e. String templates or expressions, or do we need to use a “format string” like mentioned in the challenge? I’m not exactly sure where to start for this challenge; Is there anyone that can point me in the right direction?



Hey blkxltng,

In that exercise, you are intended to implement your own “format string” of the form “(HP)(A) -> H”.
That is, you provide a string like the one above and the player’s stats are used to print something along the lines of “(HP: 100)(Aura: Green) -> Madrigal is in excellent condition!”.

We don’t intend to enforce much about your implementation, but it will almost certainly give you an opportunity to put string templates/interpolation to work.


I am a bit confused here. I assume that it should be possible to solve the exercise fully with the knowledge attained form the first three chapters. But in that case I am not sure how to tackle the problem since it seems to require some more advanced stuff. But I am not sure if I am over complicating what is expected of the answer here.



That may not be a fair assumption for the challenges in the book. Sometimes, those challenges require a bit of extra digging into the Kotlin standard library or might benefit from some additional domain knowledge from other languages.

They’re not always doable in your first pass, and that’s okay. They don’t have bearing on later content, so feel free to come back to certain challenges when you feel better-equipped.


Thx dgreenhalgh,

OK I see. That makes sense. By doing some digging I am sure I can solve it. I was just not sure whether that was “allowed” or if it should be doable within the first three chapters.

I will return with my solution :slight_smile:


So this is my first stab at this. Please let me know if there is a more Kotlin-y way to do this.

// Player status
val statusFormatString = "(HP)(A) -> H"
val formatValueHP = "HP: $healthPoints"
val formatValueA = "Aura: $auraColor"
val formatValueH = "$name $healthStatus"
val formatValueB = if (isBlessed) "YES" else "NO"

var resultString = ""
var didReadAhead = false
for (index in statusFormatString.indices) {
    if (didReadAhead) {
        didReadAhead = false

    val letter = statusFormatString[index]
    if (letter == 'A') {
        resultString += formatValueA
    } else if (letter == 'B') {
        resultString += formatValueB
    } else if (letter == 'H') {
        val nextIndex = index + 1
        if (nextIndex < statusFormatString.length && statusFormatString[nextIndex] == 'P') {
            resultString += formatValueHP
            didReadAhead = true
        } else {
            resultString += formatValueH
    } else {
        resultString += letter



I did the following making use a few CharSequence.replace() on statusFormatString.

Though there are only “H”, “HP” and “A” (ie. 3) to be replaced. I used 4 replaces. An extra one to replace “HP” to sth else avoiding the ‘H’ in “HP : nnn ” be replaced by “H” when replacing it with healthStatus.

Not pretty but will get the job done.

// Player Status

var hp = “HP: $healthPoints”
var aura = “Aura: $auraColor”
var health = “$name $healthStatus”

println (statusFormatString.replace(“HP”, “P”,false).replace(“H”,health,false).replace(“P”,hp,false).replace(“A”,aura,false))


I was puzzled about this challenge question too, and I’ve been writing code for a very long time. If I take take this instruction literally, then the challenge seems like a hard one to approach.

 For example, a status format string of: val statusFormatString = "(HP)(A) -> H"

Using a string like this to do string formatting requires string replacement, which is not optimal. This is not something you would typically do. So, I would modify statusFormatString.