-1

Hello i tried to write a hangman game in kotlin but when i click button application crashes. It might be bad kotlin syntax or something. Logcat tells me that it's error in this function

    private fun showLetter(char: Char) {
        val tv = tvWord.text.toString().toCharArray()
        for (i in tv.indices) {
            if (keyWord[i] == char) tv[i] = char
        }
        tvWord.text = String(tv)
    }

At this line

if (keyWord[i] == char) tv[i] = char
a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
Mnigos
  • 150
  • 16

1 Answers1

1

It must be an array out of bound exception.

tv is larger than keyword, so requesting keyword[i] when i is too big triggers an error.

Depending on what you want to do, check the length of both arrays before your loop, change the values on which your i variable increments, or break your loop when a condition is met.

Zekovski
  • 301
  • 1
  • 10