线程“主”中的kotlin异常java.lang.IndexOutOfBoundsException

时间:2018-12-16 13:11:27

标签: kotlin

  

线程“主”中的异常java.lang.IndexOutOfBoundsException:索引:   62,尺寸:62

我无法解决此烦人的错误。我使用index = List的大小,但是有这个例外

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")

}

我仅使用三种构造,它们受数组大小的限制 请帮我哪里出错?

1 个答案:

答案 0 :(得分:1)

替换

 0..str.size

 0 until str.size