使用.equals

时间:2018-10-16 19:25:00

标签: java string string-comparison

我要做的就是使用'.equals()'方法比较两个单个字符串属性,以确定节点对象是“起始节点”还是“目标节点”

我的代码如下:

public Node findStart(ArrayList<Node> a)
{
    if (a.size() > 0)
    {
        for (Node n : a)
        {
            if (n.getVal().equalsIgnoreCase("S"))
                n.setStart(true);
                return n;
        }
    }
    System.out.println("no starting node found");
    return null;
  }
  public Node findGoal(ArrayList<Node> a)
  {
  if (a.size()>0)
  {
      for (Node n: a)
      {
          if (n.getVal().equalsIgnoreCase("G"))
              n.setGoal(true);
            return n;
      }
  }
  System.out.println("no goal node found");
  return null;
  }

不幸的是,我得到的输出不正确,看起来像以下内容:

the starting node is: 
Ab


the goal nodes are: Ab


the goal nodes are: Ab


the goal nodes are: Ab


the goal nodes are: Ab

背景信息是:

我有一个程序可以读取文本文件,该文件包含简单矩阵形式的数据,类似于:

~          val   Ab   Cd   Ef   Gh   Ij   Kl   Mn
AB           ~    ~    1    1    ~    ~    ~    ~
CD           ~    1    ~    ~    1    ~    ~    ~
EF           G    4    ~    ~    1    ~    ~    ~
GH           ~    2    2    ~    ~    ~    8    2
IJ           S    ~    2    ~    3    ~    ~    1
KL           ~    ~    ~    2    5    ~    ~    1
MN           ~    ~    ~    ~    3    4    1    ~

文本文件矩阵表示图形,其中数字表示连接两个节点的边的距离,字母表示节点是图形的起点还是终点。

我的程序在读取此文本文件时输出以下内容:

Node AB, abbrev Ab, value ~
AB has edge to: CD labeled: 1
AB has edge to: EF labeled: 1
AB has edge from: CD labeled: 1
AB has edge from: EF labeled: 4
AB has edge from: GH labeled: 2

Node CD, abbrev Cd, value ~
CD has edge to: AB labeled: 1
CD has edge to: GH labeled: 1
CD has edge from: AB labeled: 1
CD has edge from: GH labeled: 2
CD has edge from: IJ labeled: 2

Node EF, abbrev Ef, value G
EF has edge to: AB labeled: 4
EF has edge to: GH labeled: 1
EF has edge from: AB labeled: 1
EF has edge from: KL labeled: 2

Node GH, abbrev Gh, value ~
GH has edge to: AB labeled: 2
GH has edge to: CD labeled: 2
GH has edge to: KL labeled: 8
GH has edge to: MN labeled: 2
GH has edge from: CD labeled: 1
GH has edge from: EF labeled: 1
GH has edge from: IJ labeled: 3
GH has edge from: KL labeled: 5
GH has edge from: MN labeled: 3

Node IJ, abbrev Ij, value S
IJ has edge to: CD labeled: 2
IJ has edge to: GH labeled: 3
IJ has edge to: MN labeled: 1
IJ has edge from: MN labeled: 4

Node KL, abbrev Kl, value ~
KL has edge to: EF labeled: 2
KL has edge to: GH labeled: 5
KL has edge to: MN labeled: 1
KL has edge from: GH labeled: 8
KL has edge from: MN labeled: 1

Node MN, abbrev Mn, value ~
MN has edge to: GH labeled: 3
MN has edge to: IJ labeled: 4
MN has edge to: KL labeled: 1
MN has edge from: GH labeled: 2
MN has edge from: IJ labeled: 1
MN has edge from: KL labeled: 1

1 个答案:

答案 0 :(得分:4)

findGoal方法中,您拥有

for (Node n: a)
{
    if (n.getVal().equalsIgnoreCase("G"))
        n.setGoal(true);
        return n;
}

由于没有括号,因此return n;if语句之外,因此始终在第一次迭代时执行。添加方括号,使其仅在等于G时返回:

for (Node n: a)
{
    if (n.getVal().equalsIgnoreCase("G")) {
        n.setGoal(true);
        return n;
    }
}