Marlock
Marlock

Reputation: 285

kotlin Exception in thread "main" java.lang.IndexOutOfBoundsException

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 62, Size: 62

I can't fix this annoying error. I use indexes = size of List but have this exception

import java.io.File

fun main(args: Array<String>) {
    fun markdownToHtmlSimple(inputName: String) {
        val writer = File("out.txt").bufferedWriter()
        val str = File(inputName).readLines()
        var countTagI = 0
        var countTagB = 0
        var countTagS = 0
        var openedTagI = false
        var openedTagB = false
        var openedTagS = false
        writer.write("<html>\n\t<body>\n\t\t<p>")
        for (i in 0..str.size) {
            if (str[i] == "") writer.newLine()
            else {
                Regex("""\*\*""").replace(str[i], "☺") //временная замена для облегчения поиcка
                Regex("""\~\~""").replace(str[i], "☻") //аналогично
                val currentStr = str[i].toList()
                for (j in 0..currentStr.size) {
                    when {
                        currentStr[j] == '*' -> countTagI++
                        currentStr[j] == '☺' -> countTagB++
                        currentStr[j] == '☻' -> countTagS++
                    }
                }
                if ((countTagB % 2 == 0) && (countTagI % 2 == 0) && (countTagS % 2 == 0)) for (j in 0..currentStr.size) {
                    when {
                        (currentStr[j] == '*') && !openedTagI -> {
                            writer.write("<i>")
                            openedTagI = true
                        }
                        (currentStr[j] == '*') && openedTagI -> {
                            writer.write("</i>")
                            openedTagI = false
                        }
                        (currentStr[j] == '☺') && !openedTagB -> {
                            writer.write("<b>")
                            openedTagI = true
                        }
                        (currentStr[j] == '☺') && openedTagB -> {
                            writer.write("</b>")
                            openedTagI = false
                        }
                        (currentStr[j] == '☻') && !openedTagS -> {
                            writer.write("<s>")
                            openedTagS = true
                        }
                        (currentStr[j] == '☻') && openedTagS -> {
                            writer.write("</s>")
                            openedTagS = false
                        }
                        else -> writer.write(currentStr[j].toString())
                    }
                }
            }

        }
    }
    markdownToHtmlSimple("input.txt")

}

I use only three constructions for and they are limited with size of Arrays Please help where is my error?

Upvotes: 0

Views: 336

Answers (1)

anon
anon

Reputation:

Replace

 0..str.size

with

 0 until str.size

Upvotes: 1

Related Questions