Reputation: 3952
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 !! :)
Upvotes: 9
Views: 114958
Reputation: 724
Do like this, will definitely work, CHECKED:
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{
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
}}
Upvotes: 1
Reputation: 11
Not an Android programmer but, if x.getText() returns null, .toString() produces NullPointerException. create method:
private static boolean isEmpty(Button button)
{
if (button != null)
{
final String text = button.getText();
return text != null && text.length() > 0;
}
return false;
}
then:
if (isEmpty(a) || isEmpty(b) || isEmpty(c)
......
Upvotes: 1
Reputation: 963
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.
Upvotes: 2
Reputation: 26078
I think you just need to make the second if an else if
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 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 {
//logic
}
Kind of an odd flow of execution, but I think this will fix your problem
Upvotes: 1
Reputation: 6678
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
}
}
Upvotes: 8