-2

FIXED. To get the statement to evaluate the way I wanted it to I had to write it this way:

public static Boolean pushCard(String S1, String S2) {
    Boolean result = false; 

    if ((S1.equals("fire")  || S1.equals("wind") || S1.equals("water")))
        if (!S2.equals("fire") && (!S2.equals("water") && (!S2.equals("fire"))))
            result = true;

    return result;
} //end push card method

I can not tell if this comparison is causing issues. I was using == instead of .equals but then I learned that it was the wrong way to write it. Thanks for the help!

public static Boolean pushCard(String S1, String S2) {
    Boolean result = false; 

    if ((S1.equals("fire")  || S1.equals("wind") || S1.equals("water")))
        if (!S2.equals("fire") || (!S2.equals("water") || (!S2.equals("fire"))))
            result = true;

    return result;
} //end push card method
Richard Sitze
  • 8,262
  • 3
  • 36
  • 48
Sev
  • 883
  • 1
  • 14
  • 34
  • 3
    And what is the issue? – kosa Aug 08 '13 at 02:33
  • Yes, you have it right. – morgano Aug 08 '13 at 02:34
  • All sorts of odd craziness, too much to explain.... – Sev Aug 08 '13 at 02:34
  • Look at http://stackoverflow.com/questions/4744953/string-comparison-with-logical-operator-in-java regarding using == or equals for String comparisons if that's what you're wondering. – Austin Aug 08 '13 at 02:34
  • I just realised that I have the top statement encapsulated differently than the bottom. Is one right and the other not? I am very new BTW. I know that .equals is the best for comparing strings and == is best for comparing objects. – Sev Aug 08 '13 at 02:35
  • ^ no. `.equals` is what you should use for objects. `==` is for comparing primitives (ints, booleans, etc) – gr3co Aug 08 '13 at 02:38
  • You are checking `!S2.equals("fire")` two times in your second if. Also `!S2.equals("fire") || (!S2.equals("water")` will always be true. If `S2` is not fire then `!S2.equals("fire")` will be `true`, if it is fire then `!S2.equals("water")` will be `true`. – Pshemo Aug 08 '13 at 02:39
  • == will compare references, which is usually not what you want. .equals will compare contents, which is usually what you want. – Jason C Aug 08 '13 at 02:40
  • @Nerves82 Check out my answer. Not sure if your code is going to give you what you want (I highlighted a potential logical issue). – Steve P. Aug 08 '13 at 02:49
  • Basicly I want the S1 string to be "fire" "wind" or "water" and S2 to be empty for the statement to return true. I am testing the crap out of this statement and I cannot get the second iff to evaluate properly. No matter what is held by S2, it returns "true". I think Pshemo hit on the problem but I don't understand what the proper syntax would be. – Sev Aug 08 '13 at 03:09

4 Answers4

3

Syntactically, your code will compile just fine, and the way you use .equals() method to compare strings is correct. Your use of the ! operator is also correct.

There is no guarantee that your code will not have logical errors though.

jh314
  • 27,144
  • 16
  • 62
  • 82
  • Those where the item I needed answers too. Thanks! – Sev Aug 08 '13 at 02:40
  • Minor improvement: put the constant Strings on the left side, e.g. `"fire".equals(S1)`. This allows for S1 or S2 to be null without throwing a NPE. – user949300 Aug 08 '13 at 02:54
1

The only problem I can see you have "fire" mentioned twice in your second if statement. Otherwise, any problems you might be having could be related to your logic being wrong, since your syntax is pretty much correct and your usage is proper.

gr3co
  • 893
  • 1
  • 7
  • 15
  • Oh snap! It is always the simple things you don't see. Thanks for pointing that out. That would cause problems. – Sev Aug 08 '13 at 02:39
1

It is unclear what you're asking. The second if will always be true. You probably need :

if ((S1.equals("fire")  || S1.equals("wind") || S1.equals("water")))
    if (!S2.equals("fire") && (!S2.equals("water") && (!S2.equals("wind"))))
        result = true;
fastcodejava
  • 39,895
  • 28
  • 133
  • 186
0
  public static Boolean pushCard(String S1, String S2)    
{
    Boolean result = false; 

    if (S1.equals("fire")  || S1.equals("wind") || S1.equals("water"))
        {
           (!S2.equals("fire") || !S2.equals("water"))
            result = true;
        }

    return result;

}

/end push card method

  1. you had an extra pair of brackets in the first if statement.

  2. I believe an if statement needs brackets {} when the code inside it is larger than one line.

  3. your second if statement can be altered to just !S2.equals("fire") || !S2.equals("water")

A.sharif
  • 1,977
  • 1
  • 16
  • 27
  • Just from curiosity, why you decided to create new answer and delete old one if you could just edit old one? – Pshemo Aug 08 '13 at 02:47
  • lol I accidently posted. When I tried to edit it, it wouldn't let me see the question, so I just deleted the accident post and made a new one :/ – A.sharif Aug 08 '13 at 02:48