Java';s替换方法不起作用

Java';s替换方法不起作用,java,replace,Java,Replace,我试图在几分钟内替换routeName和RouteDuration,但rpelace方法不起作用。当我在run函数外部使用它时,它正在工作,但在内部它不工作。有什么想法吗?我已经添加了我试图运行的所有代码。我很抱歉,这是如此来回。我试着缩短它,让别人更容易阅读 The Value g = {"routes":[{"routeName":"I-190 N; Electric Ave","routeDurationInMinutes":70,"routeLengthKM":83.865,"rout

我试图在几分钟内替换routeName和RouteDuration,但rpelace方法不起作用。当我在run函数外部使用它时,它正在工作,但在内部它不工作。有什么想法吗?我已经添加了我试图运行的所有代码。我很抱歉,这是如此来回。我试着缩短它,让别人更容易阅读

The Value g =  {"routes":[{"routeName":"I-190 N; Electric Ave","routeDurationInMinutes":70,"routeLengthKM":83.865,"routeLengthMiles":52.111278915,"toll":false},{"routeName":"I-190 N; Greenville Rd","routeDurationInMinutes":82,"routeLengthKM":92.569,"routeLengthMiles":57.519692099000004,"toll":false}],"startPoint":"street address","endPoint":"destination","startLatitude":"42.20115054203528","startLongitude":"-71.85038140607858","endLatitude":"42.201220801535","endLongitude":"-71.849075146695"}

 final Runnable rundatapoll = new Runnable() {

        @Override
        public void run() {
            String g;
            try {
                g = new apicall().execute().get().getBody().toString();
                System.out.println(g);
                String route1;

                String route2;
                String time1;
                String time2;
                String time3;
                ArrayList parse= new ArrayList();
                ArrayList route= new ArrayList();
                ArrayList time= new ArrayList();
                Pattern p= Pattern.compile("routeName?.+?routeLengthKM");
                Matcher m = p.matcher(g);
                Pattern p2= Pattern.compile("routeName?.+?routeDurationInMinutes");
                Pattern p3= Pattern.compile("routeDurationInMinutes?.+?routeLengthKM");
                Matcher m3= p3.matcher(g);

                while (m.find()) {
                    parse.add(m.group());
                }

                while (m3.find()){
                    time.add(m3.group());
                }

                int l=0;
                while (l<parse.size()){
                    Matcher m2 =p2.matcher((CharSequence) parse.get(l));

                    while(m2.find()){
                        route.add(m2.group());
                    }

                    l++;
                }

//////////////////////////////////////////////////////////////////////////////////////


                route1= (String) route.get(0);
                route2= (String) route.get(1);
                route1= route1.replace(",routeDurationInMinutes","");
                route1= route1.replace("routeName:","");
                route2= route2.replace(",routeDurationInMinutes","");
                route2= route2.replace("routeName:","");
                time1= (String) time.get(0);
                time2= (String) time.get(1);
                time1= time1.replace("routeDurationInMinutes:","");
                time1= time1.replace(",routeLengthKM","");
                time2= time2.replace("routeDurationInMinutes:","");
                time2= time2.replace(",routeLengthKM","");


                t1.setValue(time1);
                t2.setValue(time2);
                r1.setValue(route1);
                r2.setValue(route2);

            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }

        }
    };

>>>routeName":"I-190 N; Electric Ave","routeDurationInMinutes
值g={“路线”:[{“路线名称”:“I-190 N;电气大道”,“路线长度分钟数”:70,“路线长度公里数”:83.865,“路线长度英里数”:52.111278915,“通行费”:false},{“路线名称”:“I-190 N;格林维尔路”,“路线长度分钟数”:82,“路线长度公里数”:92.569,“路线长度英里数”:57.5196920900004,“通行费”:false},“起点”街道地址,“终点”:“目的地”、“惊人地”:“42.20115054203528”、“惊人地”:“-71.85038140607858”、“经度”:“42.201220801535”、“经度”:“-71.849075146695”}
final Runnable rundatapoll=new Runnable(){
@凌驾
公开募捐{
字符串g;
试一试{
g=新的apicall().execute().get().getBody().toString();
系统输出打印ln(g);
字符串路由1;
字符串路由2;
字符串时间1;
字符串时间2;
字符串时间3;
ArrayList parse=新的ArrayList();
ArrayList路由=新建ArrayList();
ArrayList时间=新建ArrayList();
模式p=模式.compile(“routeName?+?RouteLength km”);
匹配器m=p.匹配器(g);
模式p2=模式.compile(“routeName?+?routeDurationInMinutes”);
Pattern p3=Pattern.compile(“routeDurationInMinutes?+?RouteLength km”);
匹配器m3=p3.匹配器(g);
while(m.find()){
parse.add(m.group());
}
while(m3.find()){
time.add(m3.group());
}
int l=0;
而(l>>路线名称):“I-190 N;电气大道”,“路线数分钟”

看起来线程可能没有按照注释中的建议执行

这对我很有用:

package example;

public class Example {

     final String g="routeName:I-190 N; Electric Ave,routeDurationInMinutes";
     final Runnable rundatapoll = new Runnable() {
            @Override
            public void run() {
                  String route1=g;
                  System.out.println("BEFORE THREAD");
                  System.out.println(route1);
                  route1= route1.replace(",routeDurationInMinutes","");
                  route1= route1.replace("routeName:","");
                  System.out.println("AFTER RUNNING THREAD:");
                  System.out.println(route1);
                  System.out.println("DONE");
            }
    };

    public static void main(String[] args) {
        Example example = new Example();
        Thread thread = new Thread(example.rundatapoll);
        thread.start();
    }

}
输出:

BEFORE THREAD
routeName:I-190 N; Electric Ave,routeDurationInMinutes
AFTER RUNNING THREAD:
I-190 N; Electric Ave
DONE
ROUTES: 
Route name: I-190 N; Electric Ave
Route name: I-190 N; Greenville Rd

或者您可以使用JSON解析器。类似于:

package example;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class JsonExample {

    public static void main(String[] args) {
        try {
            String json = "{\"routes\":[{\"routeName\":\"I-190 N; Electric Ave\",\"routeDurationInMinutes\":70,\"routeLengthKM\":83.865,\"routeLengthMiles\":52.111278915,\"toll\":false},{\"routeName\":\"I-190 N; Greenville Rd\",\"routeDurationInMinutes\":82,\"routeLengthKM\":92.569,\"routeLengthMiles\":57.519692099000004,\"toll\":false}],\"startPoint\":\"street address\",\"endPoint\":\"destination\",\"startLatitude\":\"42.20115054203528\",\"startLongitude\":\"-71.85038140607858\",\"endLatitude\":\"42.201220801535\",\"endLongitude\":\"-71.849075146695\"}";
            JSONParser parser = new JSONParser();
            JSONObject obj = (JSONObject) parser.parse(json);
            JSONArray routes = (JSONArray) obj.get("routes");
            System.out.println("ROUTES: ");
            for(Object route : routes) {
                JSONObject routeObj = (JSONObject) route;
                String routeName = routeObj.get("routeName") + "";
                System.out.println("Route name: " + routeName);
            }
        } catch (Exception exp) {
            throw new RuntimeException(exp);
        }
    }

}
输出:

BEFORE THREAD
routeName:I-190 N; Electric Ave,routeDurationInMinutes
AFTER RUNNING THREAD:
I-190 N; Electric Ave
DONE
ROUTES: 
Route name: I-190 N; Electric Ave
Route name: I-190 N; Greenville Rd

@tylerik你是否执行过
Runnable
?@tylerik这是你真正的代码吗?因为下面你提到你为了这个问题修改了它。让我们看看正在运行的真实代码。你能定义“不起作用”(
replace
方法对我来说似乎工作得很好)“我只是想让每个人都容易阅读“这是正确的方向。如果完整的代码包含与您的问题无关的部分,请不要发布。花点时间发布最小但完整(可编译)的代码示例(即使从零开始也是值得的,我经常发现自己在尝试创建时找到问题的解决方案)你添加了更多的代码,但它仍然不允许我们重现你的问题(看看John的答案,看看一个最小但完整的程序是什么样子)。请重新阅读Pshemo的评论。他是100%正确的。如果你尝试按照他说的去做,你很有可能自己很快就能解决问题。