I have a quick simple technical problem: I'm trying to read this file:
S,B,C,D,E,F
0,2,1,inf,inf,inf
inf,0,2,inf,inf,inf
inf,inf,0,4,5,inf
inf,1,inf,0,inf,5
inf,inf,inf,inf,0,1
inf,inf,inf,inf,inf,0
and put each elements in some Array:
BufferedReader br = null;
FileReader fr = null;
nodes = new ArrayList<Node>();
edges = new ArrayList<Edge>();
try
{
fr = new FileReader(filePath);
br = new BufferedReader(fr);
String firstLine = "";
String line = "";
String[] words = null;
firstLine = br.readLine();
words = firstLine.split(separtor);
for (int i=0; i<words.length-1;i++)
{
nodes.add(new Node(i, words[i]));
}
int node = 0;
while ((line = br.readLine()) != null)
{
words = line.split(separtor);
for (int i=0; i<words.length-1;i++)
{
if (!words[i].equals("inf") || !words[i].equals("0"))
{
edges.add(new Edge(nodes.get(node), nodes.get(i), Integer.parseInt(words[i])));
}
}
node++;
}
}
catch (IOException e)
{
e.printStackTrace();
}
The problems come in this line : if (!words[i].equals("inf") || !words[i].equals("0"))
When the string is not "inf" or "0" then you start adding stuff but when I actually run, it still add "0" and "inf" causing an error:
Exception in thread "main" java.lang.NumberFormatException: For input string: "inf"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
I did some tests, but I can't still understand why my condition doesn't work.
Thanks in advance.