1

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.

Abra
  • 19,142
  • 7
  • 29
  • 41
Ron Jensen
  • 641
  • 7
  • 20
  • How do you want your program to respond if a user enters something that isn't obviously true or false, for example "foo" – should that be treated as "false"? or throw an exception? One approach would be: read a string, then look sequentially for all variations of "true": t, true, 1, yes, y, etc., and then treat every other input as "false". – Kaan Aug 20 '19 at 20:43
  • @kaan, I don't want to throw exceptions, as I want something self-contained. Everything that isn't explicitly true is false is definitely one way to do it. – Ron Jensen Aug 20 '19 at 20:49
  • @Nexevis, I re-phrased the question and Butiri Dan provided an answer that does not refer to a book, tool, software library, etc. – Ron Jensen Aug 20 '19 at 20:53
  • 1
    @Ron Jensen I have retracted my close flag since you no longer ask for a library, however typically you are required to include the code of what you have attempted and not just ask a general question or ask "provide the solution of this for me". – Nexevis Aug 20 '19 at 20:56

2 Answers2

1

Apache BooleanUtils have a method toBoolean working like this, however it does not parse 0/1 as far as I see in documentation

'true', 'on', 'y', 't' or 'yes' (case insensitive) will return true. Otherwise, false is returned.

m.antkowicz
  • 13,268
  • 18
  • 37
1

What about a custom implementation?

  1. Use a map and define all the values for true and false
  2. Use a list; true if the value is in the list, otherwise false
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    // Define true and false values
    Map<String, Boolean> truthiness = new HashMap<>();

    truthiness.put("yes", true);
    truthiness.put("1", true);
    truthiness.put("true", true);
    truthiness.put("t", true);
    truthiness.put("ya", true);
    truthiness.put("nein", false);
    //etc

    // Define only the true values
    List<String> trueValues = Arrays.asList("yes", "1", "true", "t", "ya");

    for(int i = 0; i < 5; ++i) {
        String value = sc.nextLine().toLowerCase();

        System.out.println("map: " + truthiness.get(value));
        System.out.println("list: " + trueValues.contains(value));
    }
}

Output

yes
map: true
list: true
1
map: true
list: true
t
map: true
list: true
ya
map: true
list: true
nein
map: false
list: false
Butiri Dan
  • 1,759
  • 5
  • 12
  • 18