0

[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!

Community
  • 1
  • 1
Dee
  • 483
  • 2
  • 11
  • 24
  • 2
    try `Double.parseDouble(tokens[2])` – Viktor K May 13 '15 at 15:29
  • 2
    `tokens` is a String array. It does not store *any* doubles. Thus you need to parse your token as a double. Most simple way: `Double.parseDouble(tokens[2]);` – GiantTree May 13 '15 at 15:29
  • @arhimed Yes I tried it now, this is not giving me any error but it is still not storing the numerical values in the arraylist – Dee May 13 '15 at 15:31

4 Answers4

5

You need to change the last argument in your constructor to a double.

list.add(new Node1(tokens[0], tokens[1], Double.parseDouble(tokens[2])));

You could also override the constructor.

public Node1(String start, String end, String cost) {
  this(start, end, Double.parseDouble(cost));
}
Dan Grahn
  • 9,044
  • 4
  • 37
  • 74
2

Use Double.parseDouble(...)

List<Node1> list = new ArrayList<Node1>();
while ((line = bufferReader.readLine()) != null) {
  String[] tokens = line.split(" ");
  list.add(new Node1(tokens[0], tokens[1], Double.parseDouble(tokens[2]))); // Error at tokens[2]              
}
ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
1

Change:

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]));             
   }

To:

List<Node1> list = new ArrayList<Node1>();
   while ((line = bufferReader.readLine()) != null) {
      String[] tokens = line.split(" ");
      list.add(new Node1(tokens[0], tokens[1], Double.parseDouble(tokens[2])));              
   }
Marcelo Luz
  • 452
  • 3
  • 8
1

String[] tokens = line.split(" "); creates an array of strings, but your node class is expecting a double for the third value. Try

list.add(new Node1(tokens[0], tokens[1], Double.parseDouble(tokens[2])));
Jeremy Barnes
  • 657
  • 4
  • 15