如何在Java中解析这个纬度/经度坐标字符串?

huangapple go评论72阅读模式
英文:

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&lt;LatLng&gt; ConvertPoints (String pointsStringInput){
        ArrayList &lt;LatLng&gt; points = new ArrayList&lt;LatLng&gt;();
        String pointsString = pointsStringInput.substring(1, pointsStringInput.length()-1);
        for (int i = 0; i &lt; pointsString.split(&quot;lat/lng: &quot;).length; i++){
            Log.w(&quot;INDEX&quot;, String.valueOf(i));
            if (!pointsString.split(&quot;lat/lng: &quot;)[i].matches(&quot;none12323232321412515152124214412&quot;)){
                points.add(new LatLng (Double.parseDouble(pointsString.split(&quot;lat/lng: &quot;)[i].split(&quot;,&quot;)[0].replace(&quot;\\(&quot;, &quot;&quot;)), Double.parseDouble(pointsString.split(&quot;lat/lng: &quot;)[i].split(&quot;,&quot;)[1].replace(&quot;\\)&quot;, &quot;&quot;))));
            }
        }
        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&lt;LatLng&gt; ConvertPoints(String pointsStringInput) {
    ArrayList&lt;LatLng&gt; points = new ArrayList&lt;&gt;();
    Matcher m = Pattern.compile(&quot;lat/lng: \\((-?\\d+\\.\\d+),(-?\\d+\\.\\d+)\\)&quot;).matcher(pointsStringInput);
    while (m.find()) {
        points.add(new LatLng(Double.parseDouble(m.group(1)), Double.parseDouble(m.group(2))));
    }
    return points;
}

使用 Stream&lt;MatchResult&gt; 的版本如下:

private List&lt;LatLng&gt; ConvertPoints(String pointsStringInput) {
    return Pattern.compile(&quot;lat/lng: \\((-?\\d+\\.\\d+),(-?\\d+\\.\\d+)\\)&quot;)
            .matcher(pointsStringInput).results()
            .map(m -&gt; new LatLng(Double.parseDouble(m.group(1)), Double.parseDouble(m.group(2))))
            .collect(Collectors.toList());
}
英文:

You did use pointsString.split(&quot;lat/lng: &quot;) 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&lt;LatLng&gt; ConvertPoints(String pointsStringInput) {
    ArrayList&lt;LatLng&gt; points = new ArrayList&lt;&gt;();
    Matcher m = Pattern.compile(&quot;lat/lng: \\((-?\\d+\\.\\d+),(-?\\d+\\.\\d+)\\)&quot;).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&lt;MatchResult&gt; is

private List&lt;LatLng&gt; ConvertPoints(String pointsStringInput) {
    return Pattern.compile(&quot;lat/lng: \\((-?\\d+\\.\\d+),(-?\\d+\\.\\d+)\\)&quot;)
            .matcher(pointsStringInput).results()
            .map(m -&gt; new LatLng(Double.parseDouble(m.group(1)), Double.parseDouble(m.group(2))))
            .collect(Collectors.toList());
}

huangapple
  • 本文由 发表于 2020年4月8日 16:42:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/61096650.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定