Home > Enterprise >  readLine() returns with nothing after a while loop with a readLine() as its condition
readLine() returns with nothing after a while loop with a readLine() as its condition

Time:05-23

I'm trying to write a code that prints out whatever the user inputted into the console, but when I insert a while loop that use the readLine() function as its condition before it, the code would always return with nothing even if the user inputted something into the console. The code is here below:

fun main() {
    while(readLine()=="A") print("A is inputted")
    print(readLine() " 123")
}

My intention was for when the user inputted (into the first readLine()) anything besides "A", the code would proceed to the next line, then prints out the next string inputted (into the second readLine()) joined with " 123".

For example: "asdfasdfaf 123", but in actuality it would just prints " 123" instead

Can anyone tell me why this is the case? And what would be the code that'll get the expected behavior? Any help will be appreciated since I'm new to this, thanks :)

EDIT:

I've found the solution a while after posting this and it's kind of weird.

fun main(args: Array<String>) {
    while(readln()=="A") print("A is inputted")
    readLine() // throwaway readLine()
    print("${readLine()} 123")
}

I might be wrong but from what I've gathered here, after the while loop is stopped, the code somehow inputted nothing --which I assume is a null-- into the second readLine(). So I just put a throwaway readLine() between those two. I still don't know why this is the case though

CodePudding user response:

By using readLine() twice, you are effectively asking for 2 pieces of input. This is what your code is currently doing:

  1. You are reading the input (using a while loop? Do you mean to use an if statement instead?)
  2. You are reading a second piece of input, and appending " 123" to the second piece of input.

Try this instead:

fun main() {
  //Some kind of for or while loop if you want to constantly take input
  val input = readLine()
  if (input == "A") {
    println("A has been input")
    println("$input 123")
  }
}

CodePudding user response:

The way it's written, you have to type your "other" input twice - once so it's seen as not "A", and again when you try to print it - you hit two readLine() calls when you type something that isn't "A"

At a guess, you're getting " 123" because you type "asdfasdfaf", which exits the while loop, but then you hit the second readLine() and you're probably just hitting Return again, wondering why it hasn't printed. So the print statement reads in the empty line you just typed, and you get " 123"

If you want to have access to the non-"A" input, you need to store it - that way you can check it during your if condition, and use it in your print statement later. Here's a way you can do it

fun main() {
    // do-while runs the loop -at least once- and checks the repeat condition at the end
    do {
        val input = readLine()
        if (input == "A") print("A is inputted")
        // this prints inside the loop, now we just need to exit it
        else print("$input 123")
    } while (input == "A") // stops looping the first time you get non-"A" input
}

which is what Cloudate9 was getting at

CodePudding user response:

I am not exactly sure what you are looking to do, but the way I read and understand your post, this is what I came up with:

fun main() {
    val input = readLine()
    if (input == "A") {
        println("A has been inputted")
    } else {
        println("$input 123")
    }
}
  • Related