【问题标题】:boolean method to check if a string can be parsed as an int用于检查字符串是否可以解析为 int 的布尔方法
【发布时间】:2019-06-29 18:50:28
【问题描述】:

请注意,在您将我的问题标记为重复之前,我引用了这个问题:What's the best way to check if a String represents an integer in Java?

我正在尝试检查来自表示邻接矩阵的图形类的图形对象。

我正在使用增强的 for 循环遍历图中的每个节点,然后使用嵌套的增强 for 循环,然后遍历从每个节点到其他节点的每个连接边。

问题是我正在处理一些只有整数值边的图,以及一些具有非整数值边的图。

所以,我需要编写一个或一系列方法来检查图形对象中的每条边是否包含可解析为整数的字符串。

我的代码非常简单和基本,但是当使用应该返回 false 的图形示例时,我得到的只是一个真实的返回值。

我的代码如下:

//method to determine if a string can be parsed as an integer greater than zero

public boolean isInteger(String str)
{
    try
    {   //if the string can be parsed as an int that's greater than zero...
        Integer.parseInt(str);
            return true;
    }
    catch(Exception e)
    {
        return false;
    }
}

//method to traverse all edges in a graph object to identify whether or not all 
//edges can be parsed as positive integers representing distances 
public void checkParsability()
{
    //while positive int edges is true and therefore all edges can be parsed as positive integers
    if (positive_int_edges)

    {
        for (Node n : this.nodeList)
        {
            for (Edge a : n.getOutgoingEdges())
                this.setPositive_int_edges(isInteger(a.getLabel()));
                //positive_int_edges = isInteger(a.getLabel());

            for (Edge a : n.getIncomingEdges())
                this.setPositive_int_edges(isInteger(a.getLabel()));
                //positive_int_edges = isInteger(a.getLabel());
        }
    }
    //return positive_int_edges;
}


public boolean isPositive_int_edges() {
    return positive_int_edges;
}

public void setPositive_int_edges(boolean positive_int_edges) {
    this.positive_int_edges = positive_int_edges;
}

邻接矩阵图如下所示:

~          val  AAA   BB    C  DDD    E
Alfa         S    ~    >    ~   99  fig
Bravo       67  999  -42    3    x   ==
Charlie      ~    ~    4    ~   yz    9
Delta       4e    3   22    x    ~  !=2
Echo       yes    ~    ~    ~  d>e   33

这应该返回 false,但由于某种原因它总是返回 true。 任何帮助将不胜感激。谢谢

编辑:positive_int_edges 是我的图形对象的布尔属性,我默认设置为 true。我的希望是遍历一系列边,直到找到第一个实例,其中字符串无法解析为int,找到第一个实例后我想停止搜索,因为我不再需要担心剩余的状态尚未遍历的边缘。

编辑 #2 这就是我尝试在我的驱动程序类中调用我的方法的方式:

System.out.println("\nthe parsability of graph g after call to parsability method is: \n");



g.checkParsability();

System.out.println(g.isPositive_int_edges());

【问题讨论】:

  • 所以你是说Integer.parseInt("~") 不会抛出异常?
  • @csmckelvey 不,我没有得到任何异常被抛出的迹象,可能是因为 catch 块?
  • 它必须抛出异常。编辑:您能否在try 中添加System.out.println(str) 并确保它们都已打印?
  • 如果抛出异常,那么 catch 块将返回你所说的没有发生的 false。这意味着像Integer.parseInt("~") 这样的东西不会抛出错误的异常——它确实会抛出异常。基本上,我无法重现您所描述的内容。
  • 很可能您的代码没有被所有输入调用。

标签: java string parsing integer boolean


【解决方案1】:

首先:如果您对实际数值不感兴趣,使用正则表达式可能更容易:

final static Pattern IsIntegerPattern = Pattern.compile("^\\d+$");

public boolean isInteger(String str) {
    return IsIntegerPattern.matcher(str).matches();
}

这样可以避免引发和捕获不必要的异常。

关于结果一直是true:这实际上不是真的。如果最后一个传入的边不是数字,它将返回false,因为您遍历所有边并检查它们是否为整数,但如果您到达具有非整数值的边,则不要中断循环。具有有效整数值的后续边将“覆盖”该信息。

所以你的实现应该是这样的:

public void checkParsability()
{
    //while positive int edges is true and therefore all edges can be parsed as positive integers
    if (positive_int_edges)

    {
        for (Node n : this.nodeList)
        {
            for (Edge a : n.getOutgoingEdges()) {
                this.setPositive_int_edges(isInteger(a.getLabel()));
                //positive_int_edges = isInteger(a.getLabel());
                break;
            ]

            for (Edge a : n.getIncomingEdges()) {
                this.setPositive_int_edges(isInteger(a.getLabel()));
                //positive_int_edges = isInteger(a.getLabel());
                break;
            }
        }
    }
    //return positive_int_edges;
}

【讨论】:

    猜你喜欢
    • 2013-05-26
    • 2013-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-30
    • 2022-07-31
    • 1970-01-01
    相关资源
    最近更新 更多