Please note that before you flag my question as duplicate, I am referencing this question: What's the best way to check if a String represents an integer in Java?
I am attempting to check graph objects which come from a graph class representing adjacency matrices.
I am using an enhanced for loop to iterate over every node in a graph, and then using a nested enhanced for loop, to then iterate over every connecting edge from each node to each other node.
The thing is that I'm dealing with some graphs that have only integer value edges, and some graphs which have non-integer valued edges.
So, I need to write a method or series of methods that can check to see if every edge in a graph object contains strings that are parsable as integers or not.
My code is very simple and basic but when using graph examples that should return false, I am getting only a true return value.
My code is as follows:
//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;
}
The adjacency matrix graph looks something like this:
~ 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
this should return false but for some reason it is always returning true. Any help would be immensely appreciated. thanks
Edit: positive_int_edges is a boolean attribute of my graph objects which I set to true as default. My hope is to traverse a series of edges until the first instance is found in which a string cannot be parsed as an int, after the first instance is found I want to stop searching since I no longer need to worry about the status of the remaining edges that haven't yet been traversed.
Edit #2 This is how I'm attempting to invoke my methods in my driver class:
System.out.println("\nthe parsability of graph g after call to parsability method is: \n");
g.checkParsability();
System.out.println(g.isPositive_int_edges());