-2

I have a string of humidity measurements, without spaces, so when i display the results on android app, i got this: 36.5036.5036.5036.2036.2036.2036.2036.10. Now i would want to graph the humidities, so i need to split the numbers and save it into array. I would need something like this:

double[] temp= {36.50, 36.50, 36.50, 36.20, 36.20, 36.20, 36.10};

This is what i tried, but its not working (the app crashes):

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]);
}

I am certain that the problem is in that part of code, because i tried to leave it out, and created the array with imaginary temperatures and it graphed just fine.

Here is the log cat:

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

Thank you for your precious time.

sinkoviak
  • 3
  • 2

1 Answers1

0

This code will look for a pattern made of 2 groups of 2 digits separated by a dot. Each match is converted to a double and added to an array.

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));
    }
}

You were close to the solution but you failed to split the responsestring as it's stated in the exception stack:

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"
jhamon
  • 3,603
  • 4
  • 26
  • 37