0

This program is a simple card game and I am hung up on one complex loop. This "fire" card needs to check its 2 neighbor gameboard slots to see if it is occupied by another card, and if so, if it is a card that it can affect. With this loop it either needs to run once successfully, or twice unsuccessfully. I thought I had it figured with the code below, but when it runs the loop unsuccessfully, the program crashes with no errors. Let me know what you think, Thanks.

This code is just the method, the main is not included.

public static void fireAction(String slotSelection)
{
    switch (slotSelection)
    {
        case "A1":
            {
                boolean x = true;
                boolean y = true;
                boolean end = false;
                while ((y == true && x == true) || (end == false))
                {   
                    int burn = roll.nextInt(2);
                    switch (burn)
                    {
                        case (0):
                            if ((newBoard.getSlotA2() == "fire") | (newBoard.getSlotA2() == "wind")){
                                newBoard.setSlotA2("BURNED");
                                end = true;}
                            else
                                x = false;
                            break;
                        case (1):
                            if ((newBoard.getSlotB1() == "fire") | (newBoard.getSlotB1() == "wind")){
                                newBoard.setSlotB1("BURNED");
                                end = true;}
                            else
                                y = false;
                            break;
                    }//end switch
                }//end while
            }//end case A1
            break;
FDinoff
  • 30,689
  • 5
  • 75
  • 96
Sev
  • 883
  • 1
  • 14
  • 34
  • are the bitwise `|`s on purpose? – CBIII Aug 03 '13 at 01:55
  • possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – jlordo Aug 03 '13 at 01:59
  • Yes, I must be using them wrong if you ask. I am under the impression that it means only one of the expressions needs to evaluate to true for the if to succeed. – Sev Aug 03 '13 at 02:02
  • @jlordo this is not a duplicate although the link is good because he/she is also not comparing strings correctly. He/she had several other issues as well Nerves82 need to use .equals for strings too. That is another error – CBIII Aug 03 '13 at 02:04

2 Answers2

1

Try to use && instead of || :

    while ((y == true && x == true) && (end == false))

and also maybe you should use || instead of | in the following code:

 if ((newBoard.getSlotA2() == "fire") | (newBoard.getSlotA2() == "wind"))
  • 2
    read [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – jlordo Aug 03 '13 at 02:00
  • Ok, that worked, all tho I am puzzled as to why. I thought using the "or" operator would make it so only one condition needed to be satisfied and not all of them. I am very new and the whole "||" and "&&" is quite confusing. Anyhow, thanks! – Sev Aug 03 '13 at 02:00
  • `&&` and `||` are logical operators `|` and `&` are bitwise operators. Don't forget to accept this answer so the user gets the rep. – CBIII Aug 03 '13 at 02:01
  • Have a look here to find the differences: http://stackoverflow.com/questions/7101992/why-do-we-usually-use-not-what-is-the-difference – karakalos10 Aug 03 '13 at 02:02
  • 1
    @Nerves82 Also take note that you are comparing strings wrong – CBIII Aug 03 '13 at 02:06
  • @Clyde Ya, I have been told to use .equals but it seems to fail whenever I try and use it so I have just been sticking with == for now even though it is the wrong way to do it. – Sev Aug 03 '13 at 02:34
  • 1
    @Nerves82 No you don't understand. Using == will compare the strings as objects, not string values. Two objects are not the same, unless one object is a reference to the other. You need to understand this quick if you want to grow as a programmer. http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – CBIII Aug 03 '13 at 02:47
  • Roger that CBill. I am starting to use the proper syntax now. Thanks. – Sev Aug 05 '13 at 03:08
0

You could use a for loop instead of while to prevent it from looping infinitely. Also, setting end to true will not necessarily end the loop, since x and y could still be true. A better condition would be:

    while((x && y) && (!end)){
JSutton
  • 71
  • 5
  • you can use a for and while loop interchangeably. – CBIII Aug 03 '13 at 01:53
  • I thought about using a for loop but then there is the off chance that the randomizer will roll the same number every time through the loop. – Sev Aug 03 '13 at 01:53
  • Wouldn't while((x && y) && (!end)) need to have all three conditions satisfied before the loop would end? – Sev Aug 03 '13 at 01:55
  • No, if either x or y is false or end is true, it will end the loop. Both the expression (x && y) and (!end) must evaluate to true for the loop to continue. – JSutton Aug 03 '13 at 01:58