0

I am trying to learn kotlin, and I came across a library called klaxon for parsing JSON. If the value I get from input is null, I want the program to keep repeating itself. Otherwise if the key does exist that I have inputted, I want the program to break. My program is not working how I would expect it to, though if I remove break, the program will loop forever even if the value is null.

Here is my code:

import java.net.*
import com.beust.klaxon.*
import java.util.*
import kotlin.text.*
 
fun webRequest(url: String) {
    val uri = URL(url).readText()
    val parser: Parser = Parser.default()
    val stringBuilder: StringBuilder = StringBuilder(uri)
    val json: JsonObject = parser.parse(stringBuilder) as JsonObject
 
    println(json)
    val input: String? = readLine()
    do {
        println(json)
        if (input == null) {
            println("value doesn't exist")
        } else {
            println(json.string(input))
            break
        }
    } while (true)
}
 
fun main() {
    webRequest("https://api.github.com")
}
Aplet123
  • 33,825
  • 1
  • 29
  • 55
dps910
  • 3
  • 1

1 Answers1

1

You're reading input only once. You should do readline() inside the loop to get desired behavior:

var input: String? = readLine()
while (input.isNullOrEmpty()) {
    println("value doesn't exist")
    input = readLine()
}
println(json.string(input))