当前位置: 首页 > 知识库问答 >
问题:

在使用自定义检查异常的同时使用StringTokenizer评估用户输入

仇经武
2023-03-14

该项目的重点是创建一个排序查询,用户使用GUI界面搜索报告,应用程序输出所有相关数据。

例:报告质量>3处的所有内容

我使用StringTokenizer对象分解字符串并计算每个令牌。第一个令牌必须是report,第二个令牌必须是all,第三个令牌必须是where,第四个令牌必须是quality、basePrice或numInStock,第五个令牌必须是关系运算符(><<=>===)。我们接到指示,如果任何令牌与它们应该是的不匹配,就抛出自定义检查异常。到目前为止,我已经计算了每个令牌,并且抛出异常,如果预期的令牌不是它应该是的。

下面是我计算每个令牌的代码,以检查它的格式是否正确:

public void detectUserInput(String input) throws MissingInputException
    {
        if (input.equals(""))
        {
            System.out.println("Null input");
            throw new MissingInputException();
        }
        else 
        {
            System.out.println("Input is not null");
        }

    }//end detectUserInput 

    public void countTokens(String input) throws IncorrectFormatException
    {
        StringTokenizer tokenLength = new StringTokenizer(input, " ,");

        if (tokenLength.countTokens() < 6)
        {
            throw new IncorrectFormatException();
        }
    }//end countTokens

    public void evaluateTokens(String input) throws IllegalStartOfQueryException,
                                                    InvalidSelectorException,
                                                     InvalidQualifierException,
                                                      InvalidLValueException,
                                                       InvalidOperatorException
    {
        StringTokenizer testTokens = new StringTokenizer(input, " ,");

        if (!testTokens.nextToken().equalsIgnoreCase("report"))
        {
            throw new IllegalStartOfQueryException();
        }
        else if (!testTokens.nextToken().equalsIgnoreCase("all"))
        {
            throw new InvalidSelectorException();
        }
        else if (!testTokens.nextToken().equalsIgnoreCase("where"))
        {
            throw new InvalidQualifierException();
        }
        else if (!testTokens.nextToken().matches("quality|numInStock|basePrice"))
        {
            throw new InvalidLValueException();
        }
        else if (!testTokens.nextToken().matches(">|<|>=|<=|=="))
        {
            throw new InvalidOperatorException();
        }

        //here is where I try to take the relational operator
        //and dump it into optok, after all the previous input
        //has been validated, but it doesnt work :(
        while (testTokens.hasMoreTokens())
        {
            tok = testTokens.nextToken();

            if (tok.matches("<|>|>=|<=|=="))
            {
                optok = tok;
            }
        }

    }//end evaluateTokens

下面是我的程序的ActionPerformed(),当用户将查询键入TextField并按Go!JButton:

private class ButtonHandler implements ActionListener
{
    public void actionPerformed(ActionEvent ev)
    {
        if (ev.getSource() == goBtn)
        {
            input = queryFld.getText();

            try
            {
                detectUserInput(input);
                countTokens(input);
                evaluateTokens(input);
            }
            catch (MissingInputException mie)
            {
                errorFld.setText("Enter an expression");
                queryFld.setText("");
                System.err.println(mie);
                mie.printStackTrace();
            }
            catch (IncorrectFormatException ife)
            {
                errorFld.setText("Too few terms");
                queryFld.setText("");
                System.err.println(ife);
                ife.printStackTrace();
            }
            catch (IllegalStartOfQueryException isqe)
            {
                errorFld.setText("Word REPORT expected");
                queryFld.setText("");
                System.err.println(isqe);
                isqe.printStackTrace();
            }
            catch (InvalidSelectorException ise)
            {
                errorFld.setText("Selector must be ALL");
                queryFld.setText("");
                System.err.println(ise);
                ise.printStackTrace();
            }
            catch (InvalidQualifierException iqe)
            {
                errorFld.setText("Qualifier error - keyword WHERE missing");
                queryFld.setText("");
                System.err.println(iqe);
                iqe.printStackTrace();
            }
            catch (InvalidLValueException ilve)
            {
                errorFld.setText("Invalid query. quality, numInStock, "
                                + "or basePrice expected");
                queryFld.setText("");
                System.err.println(ilve);
                ilve.printStackTrace();
            }
            catch (InvalidOperatorException ioe)
            {
                errorFld.setText("InvalidOperatorException. < <= > >= == expected");
                queryFld.setText("");
                System.err.println(ioe);
                ioe.printStackTrace();
            }


        }
    }//end actionPerformed
}//end ButtonHandler

如果这看起来微不足道,我很抱歉,但出于某种原因,我真的很难弄清楚。我很感激任何投入或建议。如果我缺少任何需要的信息,请让我知道,我会尽快添加。此外,以下是该部分的说明:

提前致谢:)


共有1个答案

赫连法
2023-03-14

您没有发布stacktrace,所以我猜您没有出现异常,从您的代码中阅读,我试图理解可能会发生什么…所以我可能错了。

在我看来,你似乎在使用代币器。标记器就像一个流,一旦调用nextToken(),它就会返回标记,除非将其保存在某个地方,否则下一次调用nextToken()将使上一次调用无法访问。

所以,你在哪里做:

    else if (!testTokens.nextToken().matches("quality|numInStock|basePrice"))
    {
        throw new InvalidLValueException();
    }
    else if (!testTokens.nextToken().matches(">|<|>=|<=|=="))
    {
        throw new InvalidOperatorException();
    }
while (testTokens.hasMoreTokens()) {
    StringTokenizer testTokens = new StringTokenizer(input, " ,");

    if (!testTokens.nextToken().equalsIgnoreCase("report"))
    {
        throw new IllegalStartOfQueryException();
    }
    else if (!testTokens.nextToken().equalsIgnoreCase("all"))
    {
        throw new InvalidSelectorException();
    }
    else if (!testTokens.nextToken().equalsIgnoreCase("where"))
    {
        throw new InvalidQualifierException();
    }

    // TODO here i use local variables, since you need to use these outside this method,
    // maybe use class fields or whatever else
    String subject = testTokens.nextToken();
    String opttok = testTokens.nextToken();

    if (!subject.matches("quality|numInStock|basePrice"))
    {
        throw new InvalidLValueException();
    }
    else if (!opttok.matches(">|<|>=|<=|=="))
    {
        throw new InvalidOperatorException();
    }

    // done, now you have opttok and subject
 类似资料:
  • 本文向大家介绍Python使用自定义全局变量使用eval评估表达式,包括了Python使用自定义全局变量使用eval评估表达式的使用技巧和注意事项,需要的朋友参考一下 示例 此外,此代码不能偶然引用外部定义的名称: defaultdict例如,使用可以将未定义的变量设置为零:            

  • 我有一个操作/方法来执行对数据库的插入。它需要几个字段,由于各种原因,操作可能会失败,因为一个或多个输入不是唯一的,或者因为它们与一些需要唯一的内部记录冲突。 反对这种方法的人指出,我们开发团队知道会导致失败的每个错误情况,应该返回错误代码并使用它来处理每个情况。 我看不出检查方法有任何明显的缺点。这些错误情况很有可能发生,您绝对必须在使用saveUserInfo()的任何地方解决它们。似乎正是为

  • 找到价格最高和最低的信息: mysql> SELECT @min_price:=MIN(price),@max_price:=MAX(price) FROM shop; mysql> SELECT * FROM shop WHERE [email protected]_price OR [email protected]_price; +---------+--------+-------+ |

  • 我正在开发一个Laravel ACL系统。我的基本表是,透视表是。 我想使用我的自定义中间件检查用户权限。我试过这种方法,但效果不好。每个用户都可以访问所有具有或不具有的权限。 现在,我该如何解决这个问题。请看我的代码示例。 我的控制器。 我的中间件。 还有,我的模型方法。

  • 我的方法必须请求用户输入,检查它是否是整数,如果是,则返回该整数。我尝试了使用try-catch和inputmaschException。 当它循环时,我遇到了一个问题,如果我输入一个非整数,它会不断地抛出“无效输入”“输入整数:”而不是实际要求输入一个。

  • 问题内容: 包含多个有关将检查的异常与混合使用的问题。 虽然一些答案暗示使用其方法会导致难以阅读的用户代码。 我将使用此空间来提供可提高可读性的替代解决方案。 请注意,此问题特定于CompletableFuture。 这使我们能够提供更广泛地不扩展到lambda表达式的解决方案。 问题答案: 给定实用程序类(下面提供),用户可以无缝地抛出检查异常: 由lambda引发的任何异常(是否经过检查)都将