简体   繁体   中英

how to compare more than two Strings in Java?

I have 4 strings s1,s2,s3,s4. and i want to compare it with "yes","no" and "both" It has to be like (s1.equals("yes"));

  1. if ALL strings are equal to "yes" it should give one result.
  2. if ALL strings are equal to "no" it should give one result.
  3. if any 2 strings are equal to "yes" and another 2 strings are equal to "no" it has to give one result.
  4. if any 3 strings are equal to "yes" and 1 string equal to "no" it has to give one result.
  5. if any 3 stings are equal to "no" and 1 string is equal to "yes" it has to give one result..

How to do this comparison?

I would store those strings in a list, and use Collections utility to find the frequencies of yes and no . Then apply your conditions to number of yes and no .: -

List<String> list = new ArrayList<String>() {{
    add("yes"); add("yes"); add("no"); add("no");
}};

int yes = Collections.frequency(list, "yes");
int no = Collections.frequency(list, "no");


if (yes == 4 || yes == 0) {   // all "yes" or all "no"
    System.out.println("Operation 1");

} else if (yes == 2) {   // 2 "yes" and 2 "no"
    System.out.println("Operation 2");

} else {   // (1 "yes", 3 "no") or (1 "no", 3 "yes")
    System.out.println("Operation 3");
}

Of course, I assume that your strings can only be "yes" or "no" .

You can try this logic. This one does what you intend to do :

if(s1.equals("yes") && s2.equals("yes") && s3.equals("yes") && s4.equals("yes"))
    result1;
else if (s1.equals("no") && s2.equals("no") && s3.equals("no") && s4.equals("no"))
    result2;
else if ((s1.equals("yes") || s2.equals("yes")) && (s3.equals("no") || s4.equals("no")))
    result3;
else if((s1.equals("yes") || s2.equals("yes") || s3.equals("yes")) && s4.equals("no"))
    result4;
else if((s1.equals("no") || s2.equals("no") || s3.equals("no")) && s4.equals("yes"))
    result5;

Assuming they are either "yes" or "no", use this:

int yesCount = (s1+s2+s3+s4).replace("no", "").length() / 3;

Then base your logic on that, possibly with a switch(yesCount)

String[] str={s1,s2,s3,s4};
int yesCount=0, noCount=0;
for(int i=0;i<str.length();i++){
if("yes".equals(str[i]))
yescount++;
else if("no".equals(str[i]))
noCount++;
}
String check=""+yesCount+noCount;
switch(check){
case "40":
//do whatever
break;
case "30":
//do whatever
break;
case "20":
//do whatever
break;
case "10":
//do whatever
break;
case "31":
//do whatever
break;
case "21":
//do whatever
break;
case "11":
//do whatever
break;
case "22":
//do whatever
break;
case "12":
//do whatever
break;
case "13":
//do whatever
break;
case "04":
//do whatever
break;
case "00":
//do whatever
break;
}

Assuming string value can be either yes or no

    int result = 0;
    if("yes".equalsIgnoreCase(s0)) result++;
    if("yes".equalsIgnoreCase(s1)) result++;
    if("yes".equalsIgnoreCase(s2)) result++;
    if("yes".equalsIgnoreCase(s3)) result++;
    switch(result){
        case 0:
            System.out.println("all strings are NO");
            break;
        case 1:
            System.out.println("3 strings are NO, 1 string is YES");
            break;
        case 2:
            System.out.println("2 strings are NO, 2 strings are YES");
            break;
        case 3:
            System.out.println("1 string is NO, 3 strings are YES");
            break;
        case 4:
            System.out.println("all strings are YES");
            break;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM