-4

I am writing a code that will import a string of characters from a text file, analyze the string using a stack and determine which "language" the string belongs to based on whether or not it fits certain rules. The code below tests to see if an input follows the pattern (A^nB^)^p (where n is greater than or equal to 0). The way I have it written is to load the first set of A's and B's onto a stack, then load the second set of A's and B's onto another stack, pop the two stacks at the same time and compare the returned values. If they match, move on (until the two stacks empty at the same time, hopefully) if they don't then return false.

   public static boolean checkL4(File file) throws IOException
{
    Stack stack1 = new Stack();
    Stack stack2 = new Stack();
    Stack stack3 = new Stack();
    boolean firstCompare = true;
    boolean bStart = false;
    char w = 0;

    try (Scanner sc = new Scanner(file).useDelimiter("\\s*"))
    {
        while (sc.hasNext()){
            w = sc.next().charAt(0);
            if (w == 0) {
                return true;
            }
            if (w != 'A' && w != 'B')
            {
                return false;
            }
            if (w == 'A') {
                if(!bStart) {
                    stack1.push(w);
                    stack3.push(w);
                }
                if(bStart && stack2.isEmpty()) {
                    stack2.push(w);
                } else {
                    if (firstCompare) {
                     while (!stack1.isEmpty() || !stack2.isEmpty()) {
                         if (!stack1.isEmpty() && stack2.isEmpty())
                         {
                             return true;
                         }
                         if (stack1.isEmpty() && !stack2.isEmpty()) {
                             return false;
                         } else {
                             if (stack1.pop() == stack2.pop()) {
                                 return true;
                             } else {
                                 return false;
                             }
                         }
                     }
                     stack1.push(w);
                     firstCompare = false;
                 } else {
                     if(stack1.isEmpty()){
                         while (!stack3.isEmpty() || !stack2.isEmpty()) {
                             if (stack3.isEmpty() && !stack2.isEmpty()) {
                                 return false;
                             } else {
                                 if (stack2.isEmpty() && !stack3.isEmpty()) {
                                     return false;
                                 } else {
                                     if (stack3.pop() == stack2.pop()) {
                                         return true;
                                     } else {
                                         return false;
                                     }
                                 }
                             }
                         }
                         stack1.push(w);
                     }
                     if (stack3.isEmpty()){
                         while (!stack1.isEmpty() || !stack2.isEmpty()) {
                             if (stack1.isEmpty() && !stack2.isEmpty()) {
                                 return false;
                             } else {
                                 if (stack2.isEmpty() && !stack1.isEmpty()) {
                                     return false;
                                 } else {
                                     if (stack1.pop() == stack2.pop()) {
                                         return true;
                                     } else {
                                         return false;
                                     }
                                 }
                             }
                         }
                         stack1.push(w);
                     }
                    }
                }
            }
            if (w == 'B') {
                bStart = true;

                if(bStart && stack2.isEmpty()) {
                    stack2.push(w);
                }
                if(bStart && !stack2.isEmpty()) {
                    if (!stack1.isEmpty()) {
                        stack3.push(w);
                    }
                    if(!stack3.isEmpty()) {
                        stack1.push(w);
                    }
                }
            }
        }
    }
    return false;
}

This code works correctly for most inputs (returning true for AB and AABBBAABBB, and returning false for BBAA) but in some cases where it should return false (like ABBA and AABBCCD) it returns true. So why is it returning true for cases that are palindromes and cases where there are non A and non B letters. I know I have a statement in there that states if w (the input) is not and A and not a B, return false. This is worked in similar methods I have written, so why not this one? I have also written this to return false if the two returned values don't match (which they shouldn't if the input is a palindrome).

Jes
  • 19
  • 6

1 Answers1

0

I think you should use .isEqual to compare between two Strings instead of using ==

P.Coder
  • 94
  • 9
  • My apologies, I say string in the question (referring to the entire string from the text file) but it's actually broken down into char values. – Jes Jun 25 '16 at 08:02