Osama Yawar
Osama Yawar

Reputation: 369

Java validation using method parameters

I am trying to validate username and password fields for Signin() and Signup() using validation methods. Now when i enter the correct username, it keeps on showing the error, i dnt know why.

Validation Methods:

void signup_username_validation(String username) //Username validation for signup
{
    String user_name = ""; 
    if(user_name.length() < 6 || user_name.length() > 15)
    {
        System.out.println("Username cannot be less then 6 and greater then 15 characters");
        Signup();

    }

}

void signup_password_validation(String password) //Password validation for signup
{
    String pass = ""; 

    if(pass.length() < 6)
    {
        System.out.println("Password cannot be less then 6 characters");
        Signup();
    }

}

This is how i am calling them

    System.out.println("Enter Username: ");
    username = keyboard.next();
    signup_username_validation(username);

    System.out.println("Enter Password: ");
    password = keyboard.next();
    signup_password_validation(password);

Upvotes: 0

Views: 863

Answers (3)

Leo
Leo

Reputation: 6570

try

    public class Test {

        void signup_username_validation(String user_name) //Username validation for signup
        {
//          String user_name = ""; 
            if(user_name.length() < 6 || user_name.length() > 15)
            {
                System.out.println("Username cannot be less then 6 and greater then 15 characters");
                Signup();

            }

        }

        private void Signup() {
            // TODO Auto-generated method stub

        }

        void signup_password_validation(String pass) //Password validation for signup
        {
//          String pass = ""; 

            if(pass.length() < 6)
            {
                System.out.println("Password cannot be less then 6 characters");
                Signup();
            }

        }

    }

Upvotes: 0

Laf
Laf

Reputation: 8205

You are checking the validity of newly created variables, which you have initialized to an empty string. What you really want to do is check the validity of your methods' parameter.

void signup_username_validation(String username) //Username validation for signup
    {
    if(username.length() < 6 || username.length() > 15)
        {
        System.out.println("Username cannot be less then 6 and greater then 15 characters");
        Signup();
        }
    }

void signup_password_validation(String password) //Password validation for signup
    {
    if(password.length() < 6)
        {
        System.out.println("Password cannot be less then 6 characters");
        Signup();
        }
    }

Upvotes: 1

jonasnas
jonasnas

Reputation: 3580

You are assigning to String user_name = ""; and checking this value which is always empty

Upvotes: 7

Related Questions