0

I tried to create a unique random name generator and order generator combing both and displaying text based on each name and order. But when I run my code multiple times I get the result without an error and at (CERTAIN) times it throws IndexOutOfBoundException. I am facing difficulty in pinpointing the origin of this exception as it points out to three different places in my code as marked by me with comment.

    import java.io.File

const val TAVERN_NAME = "Taernyl's Folly"
val patronList = mutableListOf("Eli", "Murdoch", "Sophie")
val lastName = listOf("IronFoot", "Fernsworth","Baggine ")
val uniqueNameSet = mutableSetOf<String>()
val menuList = File("data/tavern-menu-data.txt").readText().split("\n")

fun main() {
  
    generator()                                                      **//at TavernKt.main(Tavern.kt:15)**

}

private fun generator() {
    var orderCount = 0
    (0..9).forEach { _ ->
        val first = patronList.shuffled().first()
        val last = lastName.shuffled().first()
        val name = "$first $last"
        uniqueNameSet += name


    }
    println(uniqueNameSet)
    while (orderCount <= 9) {
        placeOrder(                                             //at TavernKt.generator(Tavern.kt:31)
            uniqueNameSet.shuffled().first(),
            menuList.shuffled().first()
        )
        orderCount++
    }
}

private fun placeOrder(patronName: String, menuData: String) {

    val indexOfApostrophe = TAVERN_NAME.indexOf('\'')
    val tavernMaster = TAVERN_NAME.substring(0 until indexOfApostrophe)
    println("$patronName \"speaks\"  with $tavernMaster about their Order.")
    val(type,name,price) = menuData.split(",")                  //at TavernKt.placeOrder(Tavern.kt:81)
    val message = "$patronName buys a $name($type) for $price."
    println(message)
    val phrase = if (name == "Dragon's Breath") {
        "$patronName exclaims: ${toDragonSpeak("Ah, delicious $name")}"
    } else {
        "$patronName says: Thanks for the $name"
    }
    println(phrase)

    //performPurchase(price)
}

private fun toDragonSpeak(phrase: String) =
    phrase.replace(Regex("[aeiou]")) {
        when (it.value) {
            "a" -> "4"
            "e" -> "3"
            "i" -> "1"
            "o" -> "0"
            "u" -> "|_|"
            else -> it.value
        }
    }

And this is my output:

[Eli Baggine , Sophie Fernsworth, Eli Fernsworth, Sophie IronFoot, Murdoch Fernsworth, Eli IronFoot]
Sophie Fernsworth "speaks"  with Taernyl about their Order.
Sophie Fernsworth buys a goblet of la croix(meal) for 1.22.
Sophie Fernsworth says: Thanks for the goblet of la croix
Murdoch Fernsworth "speaks"  with Taernyl about their Order.
Murdoch Fernsworth buys a shirley temple(elixir) for 4.12.
Murdoch Fernsworth says: Thanks for the shirley temple
Sophie Fernsworth "speaks"  with Taernyl about their Order.
Sophie Fernsworth buys a goblet of la croix(meal) for 1.22.
Sophie Fernsworth says: Thanks for the goblet of la croix
Eli IronFoot "speaks"  with Taernyl about their Order.
Eli IronFoot buys a Dragon's Breath(shandy) for 5.91.
Eli IronFoot exclaims: Ah, d3l1c10|_|s Dr4g0n's Br34th
Murdoch Fernsworth "speaks"  with Taernyl about their Order.
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
    at java.base/java.util.Collections$SingletonList.get(Collections.java:4926)
    at TavernKt.placeOrder(Tavern.kt:81)
    at TavernKt.generator(Tavern.kt:31)
    at TavernKt.main(Tavern.kt:15)
    at TavernKt.main(Tavern.kt)

Process finished with exit code 1
  • 4
    In general you should not catch an IndexOutOfBoundsException, instead you should prevent it from occurring, by checking bounds before proceeding with an operation. In any case the problem is that `menuData.split(",")` produces only produces one item, which suggests it doesn't contain a comma-separated value (which BTW, also suggested you need to look for a more object-oriented approach). Please provide a [mre]. – Mark Rotteveel Apr 16 '21 at 11:09
  • 2
    “it points out to three different places in my code” — you might want to see [this question](https://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors). – gidds Apr 16 '21 at 11:18

0 Answers0