Home > Net >  Return list outside ForEach loop return only last item in Kotlin
Return list outside ForEach loop return only last item in Kotlin

Time:02-27

I need to return all list items, in forEach it works fine, outside the loop it only returns the last item.

fun scanAndConvertFile(): String {

val scanner = Scanner(System.`in`)
print("Enter path to file to convert: ")
val fileName: String = scanner.nextLine()

val bufferedReader: BufferedReader = File(fileName).bufferedReader()
var result = bufferedReader.use { it.readText() }

    val header = result.substring(0, result.indexOf(":61:"))
    val body = result.substring(result.indexOf(":61:"), result.lastIndexOf(":61:220131C6"))
    val footer = result.substring(result.lastIndexOf(":61:220131C6"), result.length)

    var list = body.split(":61:")
    list = list.filter { it.isNotEmpty() }
    list = list.map {
        ":61:$it"
    }

    list.forEach() {

        val part1 = it.substring(0, it.indexOf("?20"))
        var part2ToBePasted = it.substring(it.indexOf("?20"), it.indexOf("?00"))

        part2ToBePasted = part2ToBePasted.drop(3)

        val part3 = it.substring(it.indexOf("?00"), it.indexOf("?27"))
        var part4ToPast = it.substring(it.indexOf("?27"), it.indexOf("?28"))
        part4ToPast = part4ToPast.drop(3)

        val part5 = it.substring(it.indexOf("?28"), it.length)

        list = if(part4ToPast.equals("")) {
            listOf(part1.plus("?20").plus(part2ToBePasted).plus(part3).plus("?27").plus(part4ToPast).plus(part5))
        } else {
            listOf(part1.plus("?20").plus(part4ToPast).plus(part3).plus("?27").plus(part4ToPast).plus(part5))
        }

// println(list) - works good

}

val converted = header.plus(list).plus(footer)

// println(converted) - print only last element of list

return converted

}

CodePudding user response:

I tried to clean up your code a little (with no guarantee of course since I do not have any test data):

fun scanAndConvertFile(): String {
  print("Enter path to file to convert: ")
  val fileName: String = Scanner(System.`in`).nextLine()
  val bufferedReader: BufferedReader = File(fileName).bufferedReader()
  val result = bufferedReader.use { it.readText() }
  val header = result.substring(0, result.indexOf(":61:"))
  val footer = result.substring(result.lastIndexOf(":61:220131C6"), result.length)
  val list = result
    .substring(result.indexOf(":61:"), result.lastIndexOf(":61:220131C6"))
    .split(":61:")
    .filter { it.isNotEmpty() }
    .map { ":61:$it" }
    .map {
      val indexOf00 = it.indexOf("?00")
      val indexOf20 = it.indexOf("?20")
      val indexOf27 = it.indexOf("?27")
      val indexOf28 = it.indexOf("?28")
      val substring27to28 = it.substring(indexOf27, indexOf28).drop(3)
      it.substring(0, indexOf20)
        .plus("?20")
        .plus(if (substring27to28 == "") it.substring(indexOf20, indexOf00).drop(3) else substring27to28)
        .plus(it.substring(indexOf00, indexOf27))
        .plus("?27")
        .plus(substring27to28)
        .plus(it.substring(indexOf28, it.length))
    }
  return header.plus(list).plus(footer)
}

Basically you need to use map instead of forEach to return a list. map is used to transform each element of a list, while with forEach you do something to or with each element, but no list is returned.

  • Related