1

I have a .txt file where data are written like this:

ZONEIDFROM;ZONEIDTO;DISTANCETYPE;FACILITYID;DISTANCE;
1;2;C;HF;9,416667;

I want to return the distance if zoneidfrom = 1 and zoneidto = 2.

Everything seems to be okay in my code except for the distance which is a double.

public void Load(String filename, int zoneidfrom, int zoneidto, String mode) throws Exception{
    queue.clear();
    int from,to;
    double dist;
    String c_or_l;

    BufferedReader br =  new BufferedReader(new FileReader(filename));
    String ligne = br.readLine(); //First line read
    while(ligne != null){
        ligne = br.readLine(); //First line read
        String [] tab = ligne.split(";");
        ArrayList<Double> d = new ArrayList<>();
        d.add(Double.parseDouble(ligne));
        from = Integer.parseInt(tab[0]);
        to = Integer.parseInt(tab[1]);
        c_or_l = tab[2];
        dist = d.get(4);

        if(zoneidfrom == from && zoneidto == to && c_or_l.equals(mode));{
            distance(dist);
        }
    }      
}
public double distance(double db_distance){
    return db_distance;
}
Jason
  • 11,744
  • 3
  • 42
  • 46

2 Answers2

2

Firstly, your code is not parsing the correct string value for the double. You should be parsing tab[4], not the whole line.

Secondly, the double value contains a comma for the decimal separator. Double.parseDouble() does not take Locale into account.

You should use a NumberFormat to parse the double, while using an appropriate Locale.

See this answer for more details.

Community
  • 1
  • 1
Jason
  • 11,744
  • 3
  • 42
  • 46
1

ligne is string and it will be "1;2;C;HF;9,416667;" in first while loop, now you are trying to parse it to double , obviously it will throw number format exception . For distance to get you have to do, Double.parseDouble(tab[4].replace(",","."))

Ajay Sreeram
  • 171
  • 2
  • 18