Reputation: 196
I am trying to compare a user's input (in this case X or Y) to a char in Scala. My code is as follows:
var marker = 0
while (marker == 0) {
val ans1 = scala.io.StdIn.readLine()
println(ans1)
if (ans1 == 'X') {
marker = 1
}
else if (ans1 == 'Y') {
marker = 2
}
else {
println("Faulty input. Please retry: X or Y?")
}
However, the result I'm getting is always "Faulty input. Please retry: X or Y?". So I figure there must be something wrong with my comparisons.
Despite searching, I could not find the problem. I tried changing "" to '' around the chars I'm comparing to (friend's suggestion), but it did not solve the issue.
Upvotes: 0
Views: 385
Reputation: 229
Try this. A string will not equal a character in that if statement.
val ans1 = scala.io.StdIn.readChar()
Upvotes: 1