我有一串溼度測量,沒有空格,所以當我在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
感謝您的寶貴時間。
http://stackoverflow.com/a/3481842/4116091 – 2014-12-02 11:13:37
剛使用'response.split(「。」);'。 – greenapps 2014-12-02 11:18:24
如果'response.equals(「36.5036.5036.5036.2036.2036.2036.2036.2036.10」)',你的正則表達式是錯誤的,所以'response'不是分割和'parts == {response}' – jhamon 2014-12-02 11:18:42