英文:
How to parse this string of latitude/longitude coordinates in java?
问题
private ArrayList<LatLng> ConvertPoints(String pointsStringInput) {
ArrayList<LatLng> points = new ArrayList<LatLng>();
String pointsString = pointsStringInput.substring(1, pointsStringInput.length() - 1);
for (int i = 0; i < pointsString.split("lat/lng: ").length; i++) {
Log.w("INDEX", String.valueOf(i));
if (!pointsString.split("lat/lng: ")[i].matches("none12323232321412515152124214412")) {
points.add(new LatLng(
Double.parseDouble(pointsString.split("lat/lng: ")[i].split(",")[0].replace("\\(", "")),
Double.parseDouble(pointsString.split("lat/lng: ")[i].split(",")[1].replace("\\)", ""))
));
}
}
return points;
}
英文:
I have a string, pointString.
and I'm trying to get the number values from them and add them to new LatLng variables.
Heres my code
private ArrayList<LatLng> ConvertPoints (String pointsStringInput){
ArrayList <LatLng> points = new ArrayList<LatLng>();
String pointsString = pointsStringInput.substring(1, pointsStringInput.length()-1);
for (int i = 0; i < pointsString.split("lat/lng: ").length; i++){
Log.w("INDEX", String.valueOf(i));
if (!pointsString.split("lat/lng: ")[i].matches("none12323232321412515152124214412")){
points.add(new LatLng (Double.parseDouble(pointsString.split("lat/lng: ")[i].split(",")[0].replace("\\(", "")), Double.parseDouble(pointsString.split("lat/lng: ")[i].split(",")[1].replace("\\)", ""))));
}
}
return points;
}
it always crashes on I=0, with java.lang.NumberFormatException: empty String
答案1
得分: 2
你曾经使用过`pointsString.split("lat/lng: "),但从未打印出它的值。在分割时,你得到了分隔符之前和之后的部分,所以在第一个分隔符之前有一个空内容。
[, (43.8457509,-79.45568817), , (43.8457509,-79.45568817), , (43.84598041,-79.45584389), , (43.845954,-79.45585094]
更简单的方法是使用正则表达式,因为你非常清楚数据应该是lat/lng: \((-?\d+\.\d+),(-?\d+\.\d+)\)
-
\(
和\)
匹配实际括号 -
-?
匹配可能的负号 -
\d+\.\d+
匹配数值 -
lat/lng: \(-?\d+\.\d+,-?\d+\.\d+\)
最终的正则表达式,然后只需添加()
来捕获这些数值
<!-- -->
private ArrayList<LatLng> ConvertPoints(String pointsStringInput) {
ArrayList<LatLng> points = new ArrayList<>();
Matcher m = Pattern.compile("lat/lng: \\((-?\\d+\\.\\d+),(-?\\d+\\.\\d+)\\)").matcher(pointsStringInput);
while (m.find()) {
points.add(new LatLng(Double.parseDouble(m.group(1)), Double.parseDouble(m.group(2))));
}
return points;
}
使用 Stream<MatchResult>
的版本如下:
private List<LatLng> ConvertPoints(String pointsStringInput) {
return Pattern.compile("lat/lng: \\((-?\\d+\\.\\d+),(-?\\d+\\.\\d+)\\)")
.matcher(pointsStringInput).results()
.map(m -> new LatLng(Double.parseDouble(m.group(1)), Double.parseDouble(m.group(2))))
.collect(Collectors.toList());
}
英文:
You did use pointsString.split("lat/lng: ")
4 times, but never print it out, but it values the following. As you split you got the parts that are before and after the delimiter, so you have the empty content before the first one.
[, (43.8457509,-79.45568817), , (43.8457509,-79.45568817), , (43.84598041,-79.45584389), , (43.845954,-79.45585094]
The easier whould be to use regular expression, as you know perfectly how the data should be lat/lng: \((-?\d+\.\d+),(-?\d+\.\d+)\)
-
\(
and\)
to match the real parenthesis -
-?
to match eventually a minus -
\d+\.\d+
to match the values -
lat/lng: \(-?\d+\.\d+,-?\d+\.\d+\)
final regex, then just add()
for capturing the values
<!-- -->
private ArrayList<LatLng> ConvertPoints(String pointsStringInput) {
ArrayList<LatLng> points = new ArrayList<>();
Matcher m = Pattern.compile("lat/lng: \\((-?\\d+\\.\\d+),(-?\\d+\\.\\d+)\\)").matcher(pointsStringInput);
while (m.find()) {
points.add(new LatLng(Double.parseDouble(m.group(1)), Double.parseDouble(m.group(2))));
}
return points;
}
The version with Stream<MatchResult>
is
private List<LatLng> ConvertPoints(String pointsStringInput) {
return Pattern.compile("lat/lng: \\((-?\\d+\\.\\d+),(-?\\d+\\.\\d+)\\)")
.matcher(pointsStringInput).results()
.map(m -> new LatLng(Double.parseDouble(m.group(1)), Double.parseDouble(m.group(2))))
.collect(Collectors.toList());
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论