Load, read and exploit .DAT file

edited July 2016 in How To...

Hello Every-one, first message here, I do need some help on that, I'm sure it's quite simple but I've been struggling a lot on that recently "grrrr" ~X(

I have a .DAT file witch comport a list of airport and coordonates of these.

Easy I want to "look for" a certain airport and gets its coordonates back.

The format of the file is :ZZZZCORDONATEX CORDONATEY
My problem is that sometimes I got 2 spaces in between values and then got a "shifting" and mess up my values(By the way I'm sure that is the problem). I don't have any more code since I didn't get it to work as its design :(|) :(|)

This file is quite big about 13k lines. here is an example of the file.

"CYGB 49.694167-124.517778
EDFU 49.695000   9.182000
LKMT 49.696111  18.110833
CJE3 49.697495-103.800722
ETIC 49.698686  11.940175
CEY3 49.700000-113.416667
CKJ7 49.706053 -97.680525
EGJA 49.706758  -2.214481
CYQQ 49.710833-124.886667
"

thanks for the help !

Flo

Answers

  • you could load with loadStrings

    Then for loop over it and use replaceAll to replace duplicate spaces by Single spaces. Maybe repeat twice

    Then you are good to go

    for loop again over it and use split to break down the line

    Maybe fill those data into an class Airport that is held by an ArrayList

    Work with that then

  • Make a backup from your dat

  • edited July 2016 Answer ✓
    // forum.Processing.org/two/discussion/17596/load-read-and-exploit-dat-file
    // GoToLoop (2016-Jul-20)
    
    static final String FILENAME = "coords", EXT = ".dat";
    static final String DELIMS = WHITESPACE + "-";
    
    Airport[] coords;
    
    void setup() {
      String[] data = loadStrings(FILENAME + EXT);
      int len = data.length;
    
      printArray(data);
      println();
    
      coords = new Airport[len];
    
      for (int i = 0; i < len; ++i) {
        String[] elems = splitTokens(data[i]);
    
        String name = elems[0];
        float x, y;
    
        if (elems.length >= 3) {
          x = float(elems[1]);
          y = float(elems[2]);
        } else {
          elems = splitTokens(data[i], DELIMS);
          x =  float(elems[1]);
          y = -float(elems[2]);
        }
    
        coords[i] = new Airport(name, x, y);
      }
    
      printArray(coords);
      exit();
    }
    
    static class Airport {
      final String name;
      final float x, y;
    
      Airport(String airport, float xx, float yy) {
        name = airport;
        x = xx;
        y = yy;
      }
    
      @ Override String toString() {
        return name + ": [ " + x + ", " + y + " ]";
      }
    }
    
  • Well works fine thanks a lot ! Great help down here :p

Sign In or Register to comment.