Alberto Ferrantelli
Alberto Ferrantelli

Reputation: 1

How to make a program that accepts words that either start with a lowercase or uppercase letter java

I'm just messing around in java and want to make a true or false trivia, what would be the best way to make the program accept the users input whether it starts with a lowercase or uppercase letter? (e.g. a true or false statement, that will accept either "True/false" or "true/false"). I tried looking on google but I can't find anything because I don't really know how to word it correctly.

Upvotes: 0

Views: 971

Answers (2)

Vidya
Vidya

Reputation: 30320

Something along these lines should work:

Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
if (input.equalsIgnoreCase("true") || input.equalsIgnoreCase("false")) {
    System.out.println("You input " + input);
}
else {
    System.out.println("Input is invalid");
}

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533690

I would do

String input = 
if (input.equalsCaseIgnore("true")) // match any case combination of TrUe or trUE

If you are using a switch statement you can't do this but you can do

switch(input.toLowerCase()) {
   case "true": // true in any case
       break;
   case "false": // false in any case
       break;
   default:
       // handle error
       break;
}

Upvotes: 3

Related Questions