0

This is my validation for user ID number, when testing, I entered an invalid string with a length less than 10 and it sets the input and doesn't execute the else statement.

The Source Code:

private String phoneNum;

public personalInfo(String phNum) {
    setPhoneNum(phNum);
}

public String getPhoneNum() {
    return phoneNum;
}

public void setPhoneNum(String phNum) {
    if (phoneNum.startsWith("05")&&(phoneNum.length()==10)){
        phoneNum = phNum;
    }
    else throw new IllegalArgumentException ("Invalid Phone Number!");
}
1
  • How are you calling this code? Please post a MCVE. Commented Apr 22, 2016 at 14:21

2 Answers 2

1

your method looks like this...

public void setPhoneNum(String phNum) {
    if (phoneNum.startsWith("05")&&(phoneNum.length()==10)){
        phoneNum = phNum;
    }

and there you are not validating the parameter, but the variable phoneNum...

do instead:

public void setPhoneNum(String phNum) {
    if (phNum.startsWith("05")&&(phNum.length()==10)){
        phoneNum = phNum;
    }
Sign up to request clarification or add additional context in comments.

Comments

0

You should be validating the value you that you are getting (phNum) and set it to the member variable phoneNum.

if (phNum.startsWith("05")&&(phNum.length()==10)){
    phoneNum = phNum;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.