[EDIT] to clarify this is a different question: My question is about parsing String to double for a token to accept it and none of the answers to this question Java Double to String conversion without formatting match my criteria.
I have got a file in the following format:
A B 10
A C 12
A D 8
B D 5
B E 2
...
Following is the code for storing the above data in arraylist. However it is only storing the start node, end node, but not the cost.
List<Node1> list = new ArrayList<Node1>();
while ((line = bufferReader.readLine()) != null) {
String[] tokens = line.split(" ");
list.add(new Node1(tokens[0], tokens[1], tokens[2])); // Error at tokens[2]
}
Following is my Node1 class
class Node1 {
String start, end;
double cost;
public Node1(String start, String end, double cost){
this.start = start;
this.end = end;
this.cost = cost;
}
public String getStartNode() {
return start;
}
public String getEndNode(){
return end;
}
public double getCost(){
return cost;
}
}
It is giving me an ERROR at tokens[2]
as following
Incompatible types; String cannot be converted to double
I understand the error as, token is expecting String but it found double (cost) but I am not sure how to fix this. Won't tokens compatible to read double or what? If not, what should I be using instead of tokens[2] to store my double value?
I have tried googling, but can't seem to find any solution.
Sorry I am new to this stuff. Please bear with me!