0

I have multiple strings that have to have a certain value to trigger the notification, but I can't seem to find a way to combine them

String wppanswer,tbanswer,ctanswer,ssowpanswer,cppanswer;
if  (wppanswer.equals("Yes"))
            (tbanswer.equals("Yes"))
            (ctanswer.equals("Yes"))
            (ssowpanswer.equals("Yes"))
            (ssowpanswer.equals("N/A"))
            (cppanswer.equals("Yes")){

                NotificationCompat.Builder Yes =
                        new NotificationCompat.Builder(this)
                                .setSmallIcon(R.drawable.adccube)
                                .setContentTitle("Success!")
                                .setContentText("YEY! You have everything you need, proceed with work");
                int mNotificationId = 001;
                NotificationManager mNotifyMgr =
                        (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                mNotifyMgr.notify(mNotificationId, Yes.build());
            }

Thank you all in advance, if I need to provide more information, I can do

EDIT I've tried the below threads to no avail

Thread 1

Thread 2

Thread 3

MMG
  • 3,226
  • 5
  • 16
  • 43
Marcusswitch
  • 491
  • 3
  • 8
  • 11
    Have you heard of OR (`||`) and AND (`&&`)? – Eran Oct 21 '15 at 11:06
  • 2
    make it more nullsafe and swap the equals: "Yes".equals(tbanswer) However... your code will never compile. – nano_nano Oct 21 '15 at 11:08
  • @Eran i knew the OR but not the AND, also couldn't seem to phrase the question right when searching for it either. Thanks muchly – Marcusswitch Oct 21 '15 at 11:10
  • 1
    When it comes to String comparison with equals it's often better to write the comparisons the other way around. So instead of `wppanswer.equals("Yes")` you would write `"Yes".equals(wppanswer)` this doesnt solve your problem but saves you a lot of nullpointer checks. For concatenating different expressions you should use `&&` (and) or `||` (or). These are boolean operators that you can use to say something like `if (X && Y)` (both X and Y have to be true) or `if (X || Y)` (only one has to be true). – Aron_dc Oct 21 '15 at 11:10
  • @Aron_dc thanks for the advice, always good to learn something new, i shall edit my existing code, and use in the future – Marcusswitch Oct 21 '15 at 11:15
  • @Marcusswitch No problem. It's always fun to share a bit of knowledge, even if it is that basic. Which let's me puzzling how on earth you are a application developer and never heard of `||` or `&&` or seemingly boolean logic at all ?!? – Aron_dc Oct 21 '15 at 11:18
  • 2
    @Aron_dc I started this job doing something completely different. As the jobs gone on, i've been pushed into doing this kind of thing, and having to learn as i go along. Although the more i do, the more i've been enjoying it – Marcusswitch Oct 21 '15 at 11:20
  • But in waht condition you want show notification , i mean you want to satisfy all the condition ? – KishuDroid Oct 21 '15 at 11:21

2 Answers2

4

You have to use logical AND, which is && in Java, to make the if block code executed only if all the conditions are true, like:

if (wppanswer.equals("Yes") &&
        tbanswer.equals("Yes") &&
        ctanswer.equals("Yes") &&
        ssowpanswer.equals("Yes") &&
        ssowpanswer.equals("N/A") &&
        cppanswer.equals("Yes")){
    //your code here        
}

Furthermore, as it's yet said in comments, it is better to use "Yes".equals(wppanswer) style in your code, to prevent the NullPointerException if your wppanswer or any other object will be NULL, while you call it's equals() method.

Stanislav
  • 27,441
  • 9
  • 87
  • 82
1

To make code more readable, consider refactoring to the helper method:

// returns true if and only if all objects are equal to each other
public static boolean allEquals(Object first, Object... rest) {
    for (Object o : rest)
        if (!Objects.equals(first, o))  // `Objects#equals` to avoid NPE 
            return false;
    return true;
}

Then use it in main code:

if (allEquals("Yes", wppanswer, tbanswer, ctanswer, ssowpanswer, cppanswer) 
    && "N/A".equals(ssowpanswer)) {
    // trigger action
}
Alex Salauyou
  • 14,185
  • 5
  • 45
  • 67