android error while Parse string to double
enter code here
06-27 00:34:04.641: ERROR/AndroidRuntime(13633): FATAL EXCEPTION: main
06-27 00:34:04.641: ERROR/AndroidRuntime(13633): java.lang.RuntimeException: Unable to start activity ComponentInfo{yaraby.y/yaraby.y.ghost}: java.lang.NumberFormatException:
06-27 00:34:04.641: ERROR/AndroidRuntime(13633): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2669)
06-27 00:34:04.641: ERROR/AndroidRuntime(13633): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2685)
06-27 00:34:04.641: ERROR/AndroidRuntime(13633): at android.app.ActivityThread.access$2300(ActivityThread.java:126)
06-27 00:34:04.641: ERROR/AndroidRuntime(13633): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2038)
06-27 00:34:04.641: ERROR/AndroidRuntime(13633): at android.os.Handler.dispatchMessage(Handler.java:99)
06-27 00:34:04.641: ERROR/AndroidRuntime(13633): at android.os.Looper.loop(Looper.java:123)
06-27 00:34:04.641: ERROR/AndroidRuntime(13633): at android.app.ActivityThread.main(ActivityThread.java:4633)
06-27 00:34:04.641: ERROR/AndroidRuntime(13633): at java.lang.reflect.Method.invokeNative(Native Method)
06-27 00:34:04.641: ERROR/AndroidRuntime(13633): at java.lang.reflect.Method.invoke(Method.java:521)
06-27 00:34:04.641: ERROR/AndroidRuntime(13633): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
06-27 00:34:04.641: ERROR/AndroidRuntime(13633): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
06-27 00:34:04.641: ERROR/AndroidRuntime(13633): at dalvik.system.NativeStart.main(Native Method)
06-27 00:34:04.641: ERROR/AndroidRuntime(13633): Caused by: java.lang.NumberFormatException:
06-27 00:34:04.641: ERROR/AndroidRuntime(13633): at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:267)
06-27 00:34:04.641: ERROR/AndroidRuntime(13633): at java.lang.Double.parseDouble(Double.java:287)
06-27 00:34:04.641: ERROR/AndroidRun开发者_JS百科time(13633): at yaraby.y.ghost.onCreate(ghost.java:89)
06-27 00:34:04.641: ERROR/AndroidRuntime(13633): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-27 00:34:04.641: ERROR/AndroidRuntime(13633): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2633)
06-27 00:34:04.641: ERROR/AndroidRuntime(13633): ... 11 more
Line 89 is
enter code here
Double p1= Double.parseDouble(x);
what i'm receving from server is String and i want it to be double and its already a double number but recive as a string
enter code here
String x=inFromServer.readLine();
String y=inFromServer.readLine();
x=x.trim();
Double p1= Double.parseDouble(x);
y=y.trim();
Double p2= Double.parseDouble(y);
Does the string includes "." (like 123.45) or "," (like 123,45)?
You should replace it according to your locale settings. Try catch block also will work.
Print you String
x on console, also be sure to use the right decimal separator for your locale. Is it . or , ?
Test it in a small program.
Regards, Stéphane
Double.valueOf(string)
returns a Double
Double.parseDouble(string)
returns the primitive type.
Since you are assigning the result to a Double
, it might be better to use
Double.valueOf(string)
.
I can't get the code to break either way, but I'd say it's worth a shot.
精彩评论