Force down error in a calculation app(use if/if else)
i have created an app that user places numbers into editexts and the app is making some calcs and displays a number..i have 7 lines with editexts .but the 7th editext is optional for the user to fill it(i added a checkbox and if the user select it the editext is visible,else its invisible..).i have a strange problem with my code and i would like to have your help..
firstly this is my code:
apostoli.setOnClickListener(new OnClickListener() {
private AlertDialog show;
public void onClick(View arg0) {
if ((vprosvasis.getText().toString() == " ")
|| (vprosvasis.getText().length() == 0)
|| (vprosvasis2.getText().toString() == " ")
|| (vprosvasis2.getText().length() == 0)
|| (vprosvasis3.getText().toString() == " ")
|| (vprosvasis3.getText().length() == 0)
|| (vprosvasis4.getText().toString() == " ")
|| (vprosvasis4.getText().length() == 0)
|| (vprosvasis5.getText().toString() == " ")
|| (vprosvasis5.getText().length() == 0)
|| (vprosvasis6.getText().toString() == " ")
|| (vprosvasis6.getText().length() == 0)
) {
开发者_如何转开发 show = new AlertDialog.Builder(mContext).setTitle("Error")
.setMessage("Symplirwste olous tous vathmous prosvasis")
.setPositiveButton("OK", null).show();
}
else if (((vprosvasis7.getText().toString() != " ")
|| (vprosvasis7.getText().length() != 0)))
{
float genikosvathmos = (float) (( new Float(vprosvasis.getText().toString())
+ new Float(vprosvasis7.getText().toString())
+ new Float(vprosvasis2.getText().toString())
+ new Float(vprosvasis3.getText().toString())
+ new Float(vprosvasis4.getText().toString())
+ new Float(vprosvasis5.getText().toString()) + new Float(
vprosvasis6.getText().toString())) / 7);
float vprosvasisFloat = Float.parseFloat(vprosvasis.getText().toString());
float vprosvasisFloat2 = Float.parseFloat(vprosvasis2.getText().toString());
float vprosvasisFloat7 = Float.parseFloat(vprosvasis7.getText().toString());
float vprosvasisFloat5 = Float.parseFloat(vprosvasis5.getText().toString());
moria = (float) (((new Float((genikosvathmos * 8)+ (vprosvasisFloat * 1.3)+ (vprosvasisFloat2 * 0.7)) * 100)));
moria2 = (float) (((new Float((genikosvathmos * 8)+ (vprosvasisFloat7 * 1.3)+ (vprosvasisFloat5 * 0.7)) * 100)));
// Toast.makeText(thetiki.this, "Genikos vathmos"+moria ,
// Toast.LENGTH_SHORT).show();
// 1o pedio
show = new AlertDialog.Builder(mContext).setTitle("moria")
.setMessage( "1o pedio: " + moria + "\n2o pedio: "
+ moria + "\n3o pedio: " + moria
+ "\n4o pedio: " + moria
+ "\n5o pedio: "+moria2)
.setPositiveButton("OK", null).show();
}
else if ((vprosvasis.getText().toString()!= " ")
|| (vprosvasis.getText().length() != 0)
|| (vprosvasis2.getText().toString() != " ")
|| (vprosvasis2.getText().length() != 0)
|| (vprosvasis3.getText().toString() != " ")
|| (vprosvasis3.getText().length() != 0)
|| (vprosvasis4.getText().toString() != " ")
|| (vprosvasis4.getText().length() != 0)
|| (vprosvasis5.getText().toString() != " ")
|| (vprosvasis5.getText().length() != 0)
|| (vprosvasis6.getText().toString() != " ")
|| (vprosvasis6.getText().length() != 0)
&&((vprosvasis7.getText().toString()== " ")
|| (vprosvasis7.getText().length() == 0))
)
{
float genikosvathmos = (float) ((new Float(vprosvasis
.getText().toString())
+ new Float(vprosvasis2.getText().toString())
+ new Float(vprosvasis3.getText().toString())
+ new Float(vprosvasis4.getText().toString())
+ new Float(vprosvasis5.getText().toString()) + new Float(
vprosvasis6.getText().toString())) / 6);
float vprosvasisFloat = Float.parseFloat(vprosvasis.getText().toString());
float vprosvasisFloat2 = Float.parseFloat(vprosvasis2.getText().toString());
moria=(float) (((new Float ((genikosvathmos*8)+(vprosvasisFloat * 1.3)+(vprosvasisFloat2 * 0.7))*100)));
// Toast.makeText(thetiki.this, "Genikos vathmos"+moria , Toast.LENGTH_SHORT).show();
show = new AlertDialog.Builder(mContext).setTitle("moria")
.setMessage("1o pedio: "+moria+"\n2o pedio: "+moria+"\n3o pedio: "+moria+"\n4o pedio: "+moria+"\n5o pedio: -")
.setPositiveButton("OK", null).show();
}
}
});
vprosvasis,vprosvasis2
..etc is the number into the editText.i have used 3 if/else.the first checks if the 6 first editexts are empty and if no there is an alert.the second would like to check if the vprosvasis7
is not empty and then make a calculation.and finally,the third one want to check if the vprosvasis7
is used and then make a new calc..this is not working..specifically,the first if is working,the second works two,but if i havent filled the 7th editext,the 3rd if stacks and forces down my app..
your help would be really very helpfull..
EDIT
LogCat when the 7th editext is empty and i press the btn 'apostoli'
02-23 23:51:27.234: ERROR/AndroidRuntime(5739): FATAL EXCEPTION: main
02-23 23:51:27.234: ERROR/AndroidRuntime(5739): java.lang.NumberFormatException:
02-23 23:51:27.234: ERROR/AndroidRuntime(5739): at org.apache.harmony.luni.util.FloatingPointParser.parseFloat(FloatingPointParser.java:305)
02-23 23:51:27.234: ERROR/AndroidRuntime(5739): at java.lang.Float.parseFloat(Float.java:291)
02-23 23:51:27.234: ERROR/AndroidRuntime(5739): at java.lang.Float.<init>(Float.java:111)
02-23 23:51:27.234: ERROR/AndroidRuntime(5739): at kostas.menu.moria.thetiki$8.onClick(thetiki.java:396)
02-23 23:51:27.234: ERROR/AndroidRuntime(5739): at android.view.View.performClick(View.java:2461)
02-23 23:51:27.234: ERROR/AndroidRuntime(5739): at android.view.View$PerformClick.run(View.java:8888)
02-23 23:51:27.234: ERROR/AndroidRuntime(5739): at android.os.Handler.handleCallback(Handler.java:587)
02-23 23:51:27.234: ERROR/AndroidRuntime(5739): at android.os.Handler.dispatchMessage(Handler.java:92)
02-23 23:51:27.234: ERROR/AndroidRuntime(5739): at android.os.Looper.loop(Looper.java:123)
02-23 23:51:27.234: ERROR/AndroidRuntime(5739): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-23 23:51:27.234: ERROR/AndroidRuntime(5739): at java.lang.reflect.Method.invokeNative(Native Method)
02-23 23:51:27.234: ERROR/AndroidRuntime(5739): at java.lang.reflect.Method.invoke(Method.java:521)
02-23 23:51:27.234: ERROR/AndroidRuntime(5739): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
02-23 23:51:27.234: ERROR/AndroidRuntime(5739): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
02-23 23:51:27.234: ERROR/AndroidRuntime(5739): at dalvik.system.NativeStart.main(Native Method)
02-23 23:51:27.249: WARN/ActivityManager(2944): Force finishing activity kostas.menu.moria/.thetiki
02-23 23:51:27.300: INFO/(2944): dumpmesg > "/data/log/dumpstate_app_error.log"
float vprosvasisFloat7 = Float.parseFloat(vprosvasis7.getText().toString());
So, when the EditText is empty, you're essentially calling Float.parseFloat(""); The empty string is not a valid float, so the Float class throws an exception.
Check for the empty string before parsing.
if (vprosvasis7.getText().toString.equals("")) {
float vprosvasisFloat7 = Float.parseFloat(vprosvasis7.getText().toString());
}
精彩评论