简体   繁体   中英

Can multiple if statements be associated with a single else clause without using else-if?

I have a validation method in which I have to check conditions one by one and then throw error messages accordingly.

To achieve this, I am using multiple if statements, and if all the conditions are false, I should execute a task.

Here's my code:

if(s1.equals("") || s2.equals("") || s3.equals("") || s4.equals("") || s5.equals("") || s6.equals(""))      
    Toast.makeText(getApplicationContext(),"Enter all Details first", Toast.LENGTH_SHORT).show();       
if(!s5.matches("[A-Za-z0-9]+"))     
    e5.setError("Username cannot contain special characters");      
if(s5.length()<6)
    e5.setError("Username must be a minimum of 6 characters.");
if(!(s3.contains("@")&& s3.contains(".")))
    e3.setError("Enter a valid Email Id");      
if(!s6.equals(s7))
    e7.setError("Passwords dont match");
if(s2.length()!=10)     
    e2.setError("Please enter a valid 10 digit number");        
else
{
    Validate v2=new Validate();
    v2.store_values(s1,s2,s3,s4,s5,s6,s8);
    v2.execute();
}   

The problem is that the else statement is associated only with the last if statement, and the previous 5 if statements work independently.

Please note that I cannot use else-ifs, since all the fields should be validated - for example even if s3 is invalid, s6 must still be validated. Therefore, all the if statements must be evaluated, regardless of how many are evaluated to true.

How can I solve this problem?

The only way for multiple if statements to be associated with the same else statement is by using if-else-if...else , which is not what you need, since you want all the if statements to be evaluated, and if-else-if...else will evaluate the if statements only until one is evaluated to true.

Instead, you can use a flag (boolean variable) that marks the input as invalid and use it instead of the else statement to determine if the input is valid.

boolean valid = true;

if(s1.equals("") || s2.equals("") || s3.equals("") || s4.equals("") || s5.equals("") || s6.equals("")) {     
    Toast.makeText(getApplicationContext(),"Enter all Details first", Toast.LENGTH_SHORT).show();    
    valid = false;
}   
if(!s5.matches("[A-Za-z0-9]+")) {    
    e5.setError("Username cannot contain special characters");  
    valid = false;
} 
if(s5.length()<6) {
    e5.setError("Username must be a minimum of 6 characters.");
    valid = false;
} 
if(!(s3.contains("@")&& s3.contains("."))) {
    e3.setError("Enter a valid Email Id");    
    valid = false;
}   
if(!s6.equals(s7)) {
    e7.setError("Passwords dont match");
    valid = false;
} 
if(s2.length()!=10) {     
    e2.setError("Please enter a valid 10 digit number");
    valid = false;
} 
if (valid) {
    Validate v2=new Validate();
    v2.store_values(s1,s2,s3,s4,s5,s6,s8);
    v2.execute();
}

Set a boolean flag in all of your if statements. If an error occured, set it to true .

Then, you replace the else segment with another if statement which executes only if the boolean flag you declared is false.

You are using a generic datatype ( String ) to store data who's values are bound by some requirements. It decreases the maintainability of your code.

A better approach would be to create datatypes which store the data. That datatype would then have additional validation logic which you could use, such as by having logic in place to reject setting the value of that datatype to something which violate your specifications. An exmaple of such a datatype would be Password . Password password = new Password("abcd"); could then throw an exception given that your specifications state that a password must be of length 5 or more characters. Futher more, you could ensure that the password only exists in memory as plain text for a very short duration, by having the constructor hash it immediately after it has been validated.

If you are using Java EE, see javax.validation.Validator for a yet another approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM