繁体   English   中英

用于检查字符串是否可以解析为 int 的布尔方法

[英]boolean method to check if a string can be parsed as an int

请注意,在您将我的问题标记为重复之前,我参考了这个问题: 在 Java 中检查 String 是否代表整数的最佳方法是什么?

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

我使用增强的 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());

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

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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM