开发者

make calculation and toast it---error

in my app i have three editText where the user can place a number.then.when the user press a button,i get the result of my calculation into another editext.

this is my code,but the app forces down..any help please?

code:

 float result = (float) ((((new Double(input11.getText().toString())
                                                    + new Double(input21.getText().toString()))/2)*0.3)+ (new Double(input31.getText().toString())*0.7));

                                    vprosvasis.setText(Float.toString(result));}

calculation:

 float genikosvathmos = (float) ((new Double(vprosvasis.getText().toString())+new Double(vprosvasis7.getText().toString())+ new Double(vprosvasis2.getText().toString())+new Double(vprosvasis3.getText().toString())
                                                        +new Double(vprosvasis4.getText().toString())+new Double(vprosvasis5.getText().toString())+new Double(vprosvasis6.getText().toString()))/7);


                                 float moria=(float) (((new Float ((genikosvathmos*8)+("vprosvasis * 1.3")+("vprosvasis2 * 0,7"))*100)));       

                                 Toast.makeText(thetiki.this, "Genikos vathmos"+moria , Toast.LENGTH_SHORT).show();

logCat:

02-23 13:57:50.255: WARN/dalvikvm(20923): threadid=1: thread exiting with uncaught exception (group=0x4001d7d0)
02-23 13:57:50.364: ERROR/AndroidRuntime(20923): FATAL EXCEPTION: main
02-23 13:57:50.364: ERROR/AndroidRuntime(20923): java.lang.NumberFormatException: 
02-23 13:57:50.364: ERROR/AndroidRuntime(20923):     at org.apache.harmony.luni.util.FloatingPointParser.parseFltImpl(Native Method)
02-23 13:57:50.364: ERROR/AndroidRuntime(20923):     at org.apache.harmony.luni.util.FloatingPointParser.parseFloat(FloatingPointParser.java:321)
02-23 13:57:50.364: ERROR/AndroidRuntime(20923):     at java.lang.Float.parseFloat(Float.java:291)
02-23 13:57:50.364: ERROR/AndroidR开发者_JS百科untime(20923):     at java.lang.Float.<init>(Float.java:111)
02-23 13:57:50.364: ERROR/AndroidRuntime(20923):     at kostas.menu.moria.thetiki$8.onClick(thetiki.java:372)
02-23 13:57:50.364: ERROR/AndroidRuntime(20923):     at android.view.View.performClick(View.java:2461)
02-23 13:57:50.364: ERROR/AndroidRuntime(20923):     at android.view.View$PerformClick.run(View.java:8888)
02-23 13:57:50.364: ERROR/AndroidRuntime(20923):     at android.os.Handler.handleCallback(Handler.java:587)
02-23 13:57:50.364: ERROR/AndroidRuntime(20923):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-23 13:57:50.364: ERROR/AndroidRuntime(20923):     at android.os.Looper.loop(Looper.java:123)
02-23 13:57:50.364: ERROR/AndroidRuntime(20923):     at android.app.ActivityThread.main(ActivityThread.java:4627)
02-23 13:57:50.364: ERROR/AndroidRuntime(20923):     at java.lang.reflect.Method.invokeNative(Native Method)
02-23 13:57:50.364: ERROR/AndroidRuntime(20923):     at java.lang.reflect.Method.invoke(Method.java:521)
02-23 13:57:50.364: ERROR/AndroidRuntime(20923):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
02-23 13:57:50.364: ERROR/AndroidRuntime(20923):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
02-23 13:57:50.364: ERROR/AndroidRuntime(20923):     at dalvik.system.NativeStart.main(Native Method)
02-23 13:57:50.399: WARN/ActivityManager(2944):   Force finishing activity kostas.menu.moria/.thetiki


You're getting a NumberFormatException. This is because your float contains Strings and can't be parsed, here: new Float ((genikosvathmos*8)+("vprosvasis * 1.3")+("vprosvasis2 * 0,7"))*100))

Simply get rid of the ""

Edit:

Furthermore, as vprosvasis etc, are EditTexts or some other kind of input you must convert their values in a proper way to to be able to do calculations with them. One possibility is:

float vprosvasisFloat = Float.parseFloat(vprosvasis.getText().toString());

Then you can perform your calculations using vprosvasisFloat instead of vprosvasis


This line is really strange, you used quotes where you shouldn't, and you used "," as a decimal delimiter. There's also some useless brackets :

float moria = (float) (((new Float ((genikosvathmos*8)+("vprosvasis * 1.3")+("vprosvasis2 * 0,7"))*100)));

You should write

float moria = (float) ((genikosvathmos * 8.0 + vprosvasis * 1.3 + vprosvasis2 * 0.7) * 100.0);


convert moria to string and then do the Tost..

String.valueOf(moria)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜