当用户输入括号时,Java程序会崩溃

当用户输入括号时,Java程序会崩溃,java,input,arraylist,stringbuilder,Java,Input,Arraylist,Stringbuilder,我正在制作一个简单的计算器,但是在我能够做到这一点之前,我需要制作一个算法来将用户输入转换为一致的格式。用户可以输入数字、运算符以及括号。程序在处理数字和运算符时没有问题,但出于某种未知的原因,每当循环遇到括号时,它就会抛出一个错误 在过去的几个小时里,我一直在尝试调试代码,但我似乎不明白为什么会发生这种情况 /** These are the possible operators */ private static final String OPERATORS = "+-/*%^()[]{}"

我正在制作一个简单的计算器,但是在我能够做到这一点之前,我需要制作一个算法来将用户输入转换为一致的格式。用户可以输入数字、运算符以及括号。程序在处理数字和运算符时没有问题,但出于某种未知的原因,每当循环遇到括号时,它就会抛出一个错误

在过去的几个小时里,我一直在尝试调试代码,但我似乎不明白为什么会发生这种情况

/** These are the possible operators */
private static final String OPERATORS = "+-/*%^()[]{}";

/** This is an ArrayList of all the discrete
    things (operators/operands) making up an input.
    This is really just getting rid of the spaces,
    and dividing up the "stuff" into manageable pieces.
*/
static ArrayList<String> input = new ArrayList<String>();

public static ArrayList inputCleaner(String postfix) {
    StringBuilder poop = new StringBuilder();
    String doody = postfix.replace(" ", "");
    try {
        for (int i = 0; i < doody.length(); i++) {
            char c = doody.charAt(i);
            boolean isNum = (c >= '0' && c <= '9');
            if (isNum) {
                poop.append(c);
                if (i == doody.length() - 1) {
                    input.add(poop.toString());
                    poop.delete(0, poop.length());
                }
            }
            else if (c == '.') {
                for (int j = 0; j < poop.length(); j++) {
                    if (poop.charAt(j) == '.') {
                        throw new SyntaxErrorException("You can't have two decimals in a number.");
                    }
                    else if (j == poop.length() - 1) {
                        poop.append(c);
                    }
                } 
                if (i == doody.length() - 1) {
                    throw new SyntaxErrorException("You can't end your equation with a decimal!");
                }
            }
            else if (OPERATORS.indexOf(c) != -1 && poop.length() != 0) {
                input.add(poop.toString());
                poop.delete(0, poop.length());
                poop.append(c);
                input.add(poop.toString());
                poop.delete(0, poop.length());  
            }
            else {
                throw new SyntaxErrorException("Make sure your input only contains numbers, operators, or parantheses/brackets/braces.");
            }
        }
        return input;
    }
    catch (SyntaxErrorException exc) {
        System.out.println("That didn't work, something was wrong with the syntax.");
        return input;
    }
}

public static void main(String[] args) {
    ArrayList test = new ArrayList();
    Scanner f = new Scanner(System.in);
    System.out.println("Please insert an argument: \n");
    String g = f.nextLine();
    test = inputCleaner(g);

    for (int z = 0; z < test.size(); z++) {
        System.out.println(test.get(z));
    }
}
/**这些是可能的操作员*/
私有静态最终字符串运算符=“+-/*%^()[]{}”;
/**这是所有离散变量的数组列表
组成输入的事物(运算符/操作数)。
这真的只是为了摆脱空间,
把“东西”分成可管理的部分。
*/
静态ArrayList输入=新建ArrayList();
公共静态ArrayList inputCleaner(字符串后缀){
StringBuilder poop=新的StringBuilder();
字符串doody=postfix.replace(“,”);
试一试{
对于(int i=0;i布尔值isNum=(c>='0'&&c我知道我的答案不是很好,因为我没有很好的解释(由于工作时大脑过热),但这种变化不会像您的情况那样引发异常(可能是您的答案;)


而不是使用
poop.length()!=0
在这种情况下,我只是简单地将其更改为
poop!=null
然后瞧……现在没有
异常
了。如果你能解释一下,请发表评论。

你到底捕捉到了什么错误?抛出了哪一行?抛出了什么错误?抛出了SyntaxErrorException。奇怪的是出现了错误,然后是prGram将打印ArrayList的内容,直到括号(不包括括号)。只要不包含括号,就不会抛出错误。该程序对括号的处理似乎与其他运算符不同。这就是我感到困惑的原因。该程序在-+*/%^下运行良好。请使用IDE并进行调试。这就是我一直在做的。感谢您的检查!该问题已在我的或的注释部分修复原始帖子。我很感激你的关注,这是因为声誉的缺失,只是我无法在那里发表评论。我认为我上面的回答介于评论和实际答案之间。我会记住你的建议。