Java 为什么我的代码在应该返回false的情况下返回true?

Java 为什么我的代码在应该返回false的情况下返回true?,java,Java,我正在编写一个代码,该代码将从文本文件导入一个字符串,使用堆栈分析该字符串,并根据该字符串是否符合某些规则来确定该字符串属于哪种“语言”。下面的代码测试输入是否遵循模式(A^nB^)^p(其中n大于或等于0)。我写的方法是将第一组A和B加载到堆栈上,然后将第二组A和B加载到另一个堆栈上,同时弹出两个堆栈并比较返回值。如果它们匹配,则继续(希望两个堆栈同时清空),如果它们不匹配,则返回false public static boolean checkL4(File file) throws

我正在编写一个代码,该代码将从文本文件导入一个字符串,使用堆栈分析该字符串,并根据该字符串是否符合某些规则来确定该字符串属于哪种“语言”。下面的代码测试输入是否遵循模式(A^nB^)^p(其中n大于或等于0)。我写的方法是将第一组A和B加载到堆栈上,然后将第二组A和B加载到另一个堆栈上,同时弹出两个堆栈并比较返回值。如果它们匹配,则继续(希望两个堆栈同时清空),如果它们不匹配,则返回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;
}

此代码对于大多数输入都能正常工作(对于AB和AABBBAABBBB返回true,对于BBAA返回false),但在某些情况下,如果它应该返回false(如ABBA和AABBCCD),则返回true。那么,为什么回文和非A和非B字母的情况会返回真值呢。我知道我在那里有一个声明,声明如果w(输入)不是,a和B都不是,返回false。这是在类似的方法,我写了,所以为什么不这一个?我写这篇文章也是为了在两个返回值不匹配时返回false(如果输入是回文,则不应返回false)。

我认为您应该使用。isEqual在两个字符串之间进行比较,而不是使用==

您不应该使用原始泛型对象,因此将
堆栈更改为
堆栈
。我很抱歉,我在问题中说string(指文本文件中的整个字符串),但实际上它被分解为char值。