0

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);
1
  • I'de suggest using keyboard.nextLine(); instead of keyboard.next(); if you expect the user to hit enter when they are done typing. Commented Jan 30, 2014 at 22:02

3 Answers 3

7

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

Sign up to request clarification or add additional context in comments.

4 Comments

He did the same thing for the password.
It asks for initialization. How should i initialize the variable user_name then?
use username param variable rather than create new user_name (also check for null)
@Jeff Craine You passed in the username variable and didn't ever use it. Then you created another user_name variable and used that one instead.
1

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();
        }
    }

Comments

0

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();
            }

        }

    }

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.