0

I got data with latitude and longitude, if they are unknown it's -1 for lat and -1 for long and then i get the correct location. After that i want to store the file again with the correct lat and long to avoid a daily query limit. However for the purpose of learning and efficiency i don't want to do it like

id+"\t"+region+"\t"+ etc...

This is how the lines look like:

5   Canada  British Colombia    Vancouver   -1  -1

I'm guessing it must be possible with

lines[i].replaceAll("regex", lat);
lines[i].replaceAll("regex", lon);

or maybe even in one line.

What should the regex be?

clankill3r
  • 9,146
  • 20
  • 70
  • 126
  • Each line of this file should correspond to an instance of a class named (say) `City`; rather than having a `String[]`, as you apparently do, you should have a `City[]`. You'd then have logic for parsing a line of the file into a `City` instance, and for serializing a `City` instance back into a `String`. So then you would write `city[i].setLatitude(...)` and `city[i].setLongitude(...)` (or just `city[i].latitude = ...` and `city[i].longitutde = ...` if you want to be more honest about your level of encapsulation). – ruakh Mar 12 '12 at 22:45
  • Presumably you are parsing the lines in the first place, to determine whether the lat/lon is -1? If, so reconstructing the line by adding strings might be easier... – DNA Mar 12 '12 at 22:46
  • i all ready have those classes, and i load the city's from another file. This file is just for the image based on the idea and the geo cause that's not represented in the other file – clankill3r Mar 12 '12 at 22:56

2 Answers2

2

You could do it with:

line.replaceAll("^((?:[^\t]*\t){4})[^\t]*", "$1" + lat);
line.replaceAll("^((?:[^\t]*\t){5})[^\t]*", "$1" + lon);

Or both with one:

line.replaceAll("^((?:[^\t]*\t){4})[^\t]*\t[^\t]*", "$1" + lat + "\t" + lon);
Qtax
  • 33,241
  • 9
  • 83
  • 121
  • that looks quite complex :) It doesn't work by me: `String line = "5\tCanada\tBritish Colombia\tVancouver\t-1\t-1";` `float lat = -55.0545;` `float lon = 55.8473;` `line = line.replaceAll("^((?:[^\t]*\t){5})[^\t]*\t[^\t]*", "$1" + lat + "\t" + lon);` `println(line);` – clankill3r Mar 12 '12 at 23:00
  • 1
    @clankill3r: Qtax simply miscounted the fields (or perhaps didn't realize that `British Columbia` is a single value). Your code should work if you change the `{5}` to `{4}`. – ruakh Mar 12 '12 at 23:57
  • Ah yeah, I went by the title "replace content after tab 5 and 6". Thanks @ruakh, updated. – Qtax Mar 13 '12 at 00:14
1

However for the purpose of learning and efficiency i don't want to do it like

 id+"\t"+region+"\t"+ etc..

This is the best approach, I think when you are aiming for efficiency. Even more efficient might be using a StringBuilder with a predefined size.

StringBuilder sb = new StringBuilder(line.length() + 15);
sb.append(id);
sb.append('\t');
sb.append(region);
// ....
Community
  • 1
  • 1
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
  • why +4? in the string builder, since a latitude and a longitude can be quite long, so shouldn't it be longer? And what makes the string builder more effiecient over ####id+"\t"+region+"\t"+ etc.. – clankill3r Mar 12 '12 at 22:58
  • @clankill3r: Yeah, I didn't understand what is was. Thanks for noticing. I googled it and yes, indeed. Adding 15 would be better, I think. – Martijn Courteaux Mar 12 '12 at 23:00