Reputation: 317
All I'm trying to do is compare two single character string attributes using the '.equals()' method to determine if a node object is a "starting node" or a "goal node"
My code is as follows:
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;
}
unfortunately the output i'm getting is incorrect and looks like the following:
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
the background information is that:
I have a program that reads in text files which contain data in the form of simple matrices, similar to this:
~ 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 ~
the text file matrices represent graphs where the numbers represent the distance of an edge connecting two nodes and the letters denote whether or not a node is the starting point or a goal point of the graph.
my program outputs the following when reading in this text file:
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
Upvotes: 0
Views: 58
Reputation: 12819
In the findGoal
method you have
for (Node n: a)
{
if (n.getVal().equalsIgnoreCase("G"))
n.setGoal(true);
return n;
}
Since you don't have brackets, the return n;
is outside of the if
statement and thus always executed on the first iteration. Add brackets so that it only returns if it equals G
:
for (Node n: a)
{
if (n.getVal().equalsIgnoreCase("G")) {
n.setGoal(true);
return n;
}
}
Upvotes: 4