I am writing an interactive console app in Java. I need to ask the user questions with Boolean responses, however, Scanner.in.nextBoolean()
is rather brittle in that it throws exceptions on incorrect input, so wrapping it in a method seems like a good idea. I could just use a try/catch block and insist on the user typing "true" or "false" to answer and catch the typos. But then I thought I could use Scanner.in.next()
or Scanner.in.nextLine()
and evaluate the returned string for truthiness using something like parseBoolean(String s)
.
Is there a way to evaluate a string for "truthiness" beyond true/false? That is, "0"/"1" "yes"/"no" would also evaluate to true/false.