如何求和字符串双值

时间:2018-01-04 11:32:31

标签: java android

我有一个像24.5 km10.1 km30.9 km这样的字符串。

我想计算字符串中所有距离的总和,但我无法做到。我得到NumberFormatException

这是我的代码

String stringValue = getDistanceOnRoad(lat, long, lat2, long2);
double sumValue = 0.0;

Log.d("my string is", stringValue);
try {
    sumValue = Double.parseDouble(stringValue);
    Log.d("distance there plus", String.valueOf(sumValue));
} catch (NumberFormatException e){
    System.out.println("not a number");
}

我与这种方法保持距离,一切正常,但我无法计算所有距离的总和。

How to calculate distance between two locations using their longitude and latitude value

private String getDistanceOnRoad(double latitude, double longitude,
        double prelatitute, double prelongitude) {
    String result_in_kms = "";
    String url = "http://maps.google.com/maps/api/directions/xml?origin="
            + latitude + "," + longitude + "&destination=" + prelatitute
            + "," + prelongitude + "&sensor=false&units=metric";
    String tag[] = { "text" };
    HttpResponse response = null;
    try {
        HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        HttpPost httpPost = new HttpPost(url);
        response = httpClient.execute(httpPost, localContext);
        InputStream is = response.getEntity().getContent();
        DocumentBuilder builder = DocumentBuilderFactory.newInstance()
                .newDocumentBuilder();
        Document doc = builder.parse(is);
        if (doc != null) {
            NodeList nl;
            ArrayList args = new ArrayList();
            for (String s : tag) {
                nl = doc.getElementsByTagName(s);
                if (nl.getLength() > 0) {
                    Node node = nl.item(nl.getLength() - 1);
                    args.add(node.getTextContent());
                } else {
                    args.add(" - ");
                }
            }
            result_in_kms = String.format("%s", args.get(0));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result_in_kms;
}

5 个答案:

答案 0 :(得分:1)

如果您的字符串是24.5 km10.1 km30.9 km,那么在将其转换为两倍之前,您应该先分析字符串。

String stringValue = getDistanceOnRoad(lat, long, lat2, long2);
double sumValue = 0.0;

Log.d("my string is", stringValue);
try {
    String[] distances = stringValue.split(" km");
    for(String distance : distances)
    {
       sumValue = sumValue + Double.parseDouble(distance);
    }
    Log.d("distance there plus", String.valueOf(sumValue));
} catch (NumberFormatException e){
    System.out.println("not a number");
}

答案 1 :(得分:0)

好的,问题在于您尝试将String S = "10.1 km";解析为双变量。然而," km"字符串的一部分不是数字,因此java编译器无法解析它。而是为km的数量定义一个双变量:double km = 10.1和一个#34; km"的String变量。然后(当你想把它作为一个字符串)时,用操作的答案重新定义km的变量。

答案 2 :(得分:0)

试试这个

String s = "24.5 km10.1 km30.9 km";

String[] items = s.split("km");
double result = 0;

for (int i = 0 ; i < items.length ; i++) {   
    result = result + Double.parseDouble(items[i]); 
}

System.out.print("result = " + result);

答案 3 :(得分:0)

试试这个

{{1}}

答案 4 :(得分:0)

您应该按km分割,然后对值求和,这是方法:

private static double parseAndSum(String string) {

    double sumValue = 0.0;
    String[] nums = string.split("km");

    int i=0;
    for(i=0;i<nums.length;i++) {
        sumValue += Double.parseDouble(nums[i].trim());
    }
    return sumValue;
}

在这里你怎么称呼它:

String string = getDistanceOnRoad(latitude, longitude, prelatitute, prelongitude);    
double result = parseAndSum(string);
System.out.println("Total is : " + result + "km");