我有一串湿度测量,没有空格,所以当我在Android应用程序上显示结果时,我得到了这个:36.5036.5036.5036.2036.2036.2036.2036.10。 现在我想绘制湿度图,所以我需要拆分数字并将其保存到数组中。我需要这样的东西:
double[] temp= {36.50, 36.50, 36.50, 36.20, 36.20, 36.20, 36.10};
这是我尝试过的,但它不起作用(应用程序崩溃):
String parts[] = response.split("(?<=\\G.{5})");
double[] partsD = new double[parts.length];
for(int i = 0; i < parts.length; i++)
{
partsD[i] = Double.parseDouble(parts[i]);
}
我确信问题出现在代码的那一部分,因为我试图将其遗漏,并用虚构的温度创建数组,并且绘制得很好。
这是日志猫:
12-02 12:08:52.484: E/AndroidRuntime(27004): FATAL EXCEPTION: main
12-02 12:08:52.484: E/AndroidRuntime(27004): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Graph/com.Graph.second}: java.lang.NumberFormatException: Invalid double: "36.5036.5036.5036.2036.2036.2036.2036.1036.1036.1036.1036.1036.1036.1036.1036.1036.1036.1036.1036.10 "
12-02 12:08:52.484: E/AndroidRuntime(27004): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
12-02 12:08:52.484: E/AndroidRuntime(27004): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2357)
12-02 12:08:52.484: E/AndroidRuntime(27004): at android.app.ActivityThread.access$600(ActivityThread.java:153)
12-02 12:08:52.484: E/AndroidRuntime(27004): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
12-02 12:08:52.484: E/AndroidRuntime(27004): at android.os.Handler.dispatchMessage(Handler.java:99)
12-02 12:08:52.484: E/AndroidRuntime(27004): at android.os.Looper.loop(Looper.java:137)
12-02 12:08:52.484: E/AndroidRuntime(27004): at android.app.ActivityThread.main(ActivityThread.java:5226)
12-02 12:08:52.484: E/AndroidRuntime(27004): at java.lang.reflect.Method.invokeNative(Native Method)
12-02 12:08:52.484: E/AndroidRuntime(27004): at java.lang.reflect.Method.invoke(Method.java:511)
12-02 12:08:52.484: E/AndroidRuntime(27004): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
12-02 12:08:52.484: E/AndroidRuntime(27004): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
12-02 12:08:52.484: E/AndroidRuntime(27004): at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:112)
12-02 12:08:52.484: E/AndroidRuntime(27004): at dalvik.system.NativeStart.main(Native Method)
12-02 12:08:52.484: E/AndroidRuntime(27004): Caused by: java.lang.NumberFormatException: Invalid double: "36.5036.5036.5036.2036.2036.2036.2036.1036.1036.1036.1036.1036.1036.1036.1036.1036.1036.1036.1036.10"
12-02 12:08:52.484: E/AndroidRuntime(27004): at java.lang.StringToReal.invalidReal(StringToReal.java:63)
12-02 12:08:52.484: E/AndroidRuntime(27004): at java.lang.StringToReal.parseDouble(StringToReal.java:269)
12-02 12:08:52.484: E/AndroidRuntime(27004): at java.lang.Double.parseDouble(Double.java:295)
12-02 12:08:52.484: E/AndroidRuntime(27004): at com.Graph.second.onCreate(second.java:49)
12-02 12:08:52.484: E/AndroidRuntime(27004): at android.app.Activity.performCreate(Activity.java:5104)
12-02 12:08:52.484: E/AndroidRuntime(27004): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-02 12:08:52.484: E/AndroidRuntime(27004): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
12-02 12:08:52.484: E/AndroidRuntime(27004): ... 12 more
感谢您宝贵的时间。
答案 0 :(得分:0)
此代码将查找由2个2位数字组成的模式,这些数字用点分隔。每个匹配都转换为double并添加到数组中。
Pattern pattern= Pattern.compile("\\d{2}.\\d{2}");
Matcher matcher = pattern.matcher(response);
if(matcher.matches(){
double[] partsD = new double[parts.length];
for(int i = 1; i < matcher.groupCount(); i++){
partsD[i] = Double.parseDouble(matcher.group(i));
}
}
您接近解决方案,但未能将response
字符串拆分为异常堆栈中所述的字符串:
12-02 12:08:52.484: E/AndroidRuntime(27004): Caused by: java.lang.NumberFormatException: Invalid double: "36.5036.5036.5036.2036.2036.2036.2036.1036.1036.1036.1036.1036.1036.1036.1036.1036.1036.1036.1036.10"