Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use multiple conditions on the same data in an if else loop? - java/android

I have 3 edittexts with input type as number and 1 button that say Done

I have to check two conditions for the values entered in the edittexts

When I click on the button Done I should be able to do thee following things

----First is to check if the edit texts are empty or not

if ((a.getText().toString().equals("")) || (b.getText().toString().equals("")) || (c.getText().toString().equals("")))
    Toast.makeText(getApplicationContext(), "Don't leave grade points empty",0).show();

----Second is to check if the values entered are within a certain range

if((a1<5 || a1>10)||(b1<5 || b1>10)||(c1<5 || c1>10))
    Toast.makeText(getApplicationContext(), "Range should be between 15 to 25", 0).show();

both a,b,c and a1,b1,c1 represent the same values, i've just parsed the values of a,b,c to int in a1,b1,c1

----Then in the else part I should be able to use these values if they pass the above conditions.

else
{
//do something
}

My problem is how to make it work as I face a Force close error on clicking the Done button when all 3 edittext are empty. I tried to put those in if, else if and else loop and also tried if,if and else loop.

How should I code that if it doesn't satisfy both the conditions and then go to the else part?

Help !! :)

like image 739
kumareloaded Avatar asked Jun 13 '12 19:06

kumareloaded


2 Answers

if ((a.getText().toString().equals("")) || (b.getText().toString().equals("")) || (c.getText().toString().equals("")))
    Toast.makeText(getApplicationContext(), "Don't leave grade points empty",0).show();
else {
    //Convert your a/b/c.getText() to a1/b1/c1 now you know they are not empty
    if((a1<5 || a1>10)||(b1<5 || b1>10)||(c1<5 || c1>10))
        Toast.makeText(getApplicationContext(), "Range should be between 15 to 25", 0).show();
    else
    {
    //do something
    }
}
like image 91
John Avatar answered Sep 20 '22 22:09

John


I think it is because you are trying to perform some kind of check on a variable that is null. You could try:

if (a.getText() == null || b.getText() == null || c.getText() == null ){
     //Toast code
} else {
    //Convert string to int code
    if ((a1<5 || a1>10) || (b1<5 || b1>10) || (c1<5 || c1>10)){
    //Toast code
    } else {
        //Do something with numbers in the correct range code here
    }
}

It would also help to look at your logcat output. You should see a line that says: "Caused by ...." which will tell you what is causing your force close.

like image 29
thomas.cloud Avatar answered Sep 20 '22 22:09

thomas.cloud