1

I looked at the other questions like this but I couldn't find one that worked. I'm trying to check if the person replied with something meaning "yes", so I'm checking for several words that mean yes. But if they reply with "yEs", my code won't work because its case sensitive, and I don't know what to do.

Code:

String fq = JOptionPane.showInputDialog(null, "Welcome to RadinBot! Would you like to continue?", "RadinBot", JOptionPane.YES_NO_CANCEL_OPTION);

if (fq.contains("yes") ||
    fq.contains("Yeah") ||
    fq.contains("Yup") ||
    fq.contains("mhm") ||
    fq.contains("Yea") ||
    fq.contains("sure"))
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • Have you considered converting the inputted text into all lowercase with `toLowerCase` and then using `contains` on the lowercased input? Consider this: `input.toLowerCase().contains("yeah")`. Note: you'll need to convert any uppercase letters in your expected response to lowercase as well ("Yeah" turns to "yeah"). – Xetnus Sep 03 '16 at 14:17
  • yeah I have and I don't think it worked for me. but I could try again, thanks! – Radin Hakimjavadi Sep 03 '16 at 14:18
  • @tunaki I actually don't think this is a dupe, because OP wants to match one of *several* words, which has a different solution. – Bohemian Sep 03 '16 at 14:37
  • @Bohemian To me, the heart of the question is still how to make a case-insensitive contains operation. In this instance, to do one or more, OP should first know how to do one (there are a lot of different solutions), and then ask a specific new question about how to do 2 with their current solution. But the answers of that linked question can be easily adjusted with multiple words, so I don't see it as an issue. I found [this question](http://stackoverflow.com/questions/8751455/arraylist-contains-case-sensitivity) but it is specific to an `ArrayList` and I don't feel it would help OP more. – Tunaki Sep 03 '16 at 15:11

5 Answers5

4

You can just use toLowerCase() to remove the case sensitivity:

if (fq.toLowerCase().contains("yes") || fq.toLowerCase().contains("yeah") || fq.toLowerCase().contains("yup") || fq.toLowerCase().contains("mhm") || fq.toLowerCase().contains("yea") || fq.toLowerCase().contains("sure"))
Drew Kennedy
  • 4,118
  • 4
  • 24
  • 34
  • Thank you so much! the funny thing is, I thought I tried that but now when I used that it worked :D! – Radin Hakimjavadi Sep 03 '16 at 14:21
  • Any time! Be sure to read the other answers and try them out too because there are multiple ways to skin this cat, and each way will be better in different circumstances. – Drew Kennedy Sep 03 '16 at 14:24
1

Change your fq.contains("yes") to like this:fq.toLowerCase().contains("yes") so basically you are converting whatever user enters into lowercase and matching with lowercase yes. So basically you are doing. case insensitive matching.

Do same for other conditions also.

Pradeep Simha
  • 17,683
  • 18
  • 56
  • 107
1

Just put that text to lowercase. And store words in array for cleaner code.

String words[] = ["yes", "yeah", "mhm", "yup"];
boolean accept = false;
for (int i=0; i<words.length(); i++){
    if (text.toLowerCase().contains(words[i])){
        accept = true;
        break;
    }
}
if (accept){
    //do what you want to do.
}
Mike B
  • 2,756
  • 2
  • 16
  • 28
0

You should better use specialized library: org.apache.commons.lang3.StringUtils.containsIgnoreCase("ABCDEFGHIJKLMNOP", "gHi");

You shount not call toUpperCase/toLowerCase, because weird locale-sensitive things may happen:

"ß".toUpperCase() // "SS"
dveim
  • 3,381
  • 2
  • 21
  • 31
0

To check for multiple words in one statement, and do it case insensitively, use regex!

if (fq.matches("(?i).*\\b(yes|Yeah|Yup|mhm|Yea|sure)\\b.*"))

Actually, you need regex, because you don't want "I'm a yuppie" even though it contains "Yup". That's where the \b term comes in - it means "word boundary". Had your code used .equals() I would have suggested more simply:

if (fq.matches("(?i)yes|Yeah|Yup|mhm|Yea|sure"))

The term (?i) means "case insensitive".

The expression (a|b|c) is an alternation; | is a logical "OR" in regex.

You need .* either end because matches() must match the whole string to be true.

If you use .toLowerCase().contains(), you would have to call that for every word you're looking for and the code would be a train wreck.

Bohemian
  • 412,405
  • 93
  • 575
  • 722