MCGuy MCGuy - 2 months ago 12
Java Question

Make the system.in NON-CASE SENSITIVE and NON-"QUESTION MARK SENSITIVE"

I'm trying to make a siri-like program for fun. Here's part of the code:

if (output.equalsIgnoreCase("What's your name") | ("What's your name?")) {

//whatever I want here

}


This would work if I didn't have the
|
sign. Does anybody have an idea of how to fix this?

Thanks.

Answer

First that | is actually a bitwise or, to do or like you are expecting it should be ||

Second you'll want to use a regex

You can get the result you are looking for like this:

if (string.matches("(?i:What's your name\?*)")){...}

The \? escapes the question mark and the * after it says "the previous character 0 or multiple times", making the number of ?'s irrelevant

The (?i:...) indicates you want a case insensitive match for the contents.

Comments