I broke my foot and missed the first part of class where this was covered now I'm all confused.

I have a password class and I must create within this class a static method of isValid
that will accept a possible password value and return if it is valid. I have to test for a. must be at least 6 characters long
b. contain at least 1 capital letter
c. contain at least 1 lowercase letter
d. contain at least 1 digit.

public class Password
{
    // instance variables - replace the example below with your own
    public  String userPassword;

    /**
     * Constructor for objects of class Password
     */
    public Password(String password)
    {
        // initialise instance variables
        password = userPassword;

    }

    /**
     * An example of a method - replace this comment with your own
     * 
     * @param  y   a sample parameter for a method
     * @return     the sum of x and y 
     */
    public static boolean isValid()
    {
        if (userPassword == null)
        {
            throw new IllegalArgumentException("User password may not be null");
          
           
        }

        if (userPassword.length() < 6)
        {
            throw new IllegalArgumentException ("User password must be atleast 6 characters");
            
        }
        
        {
            return false;
        }
        
    }
    // put your code here
    
}

I don't know if I posted the code correctly or not, if not please let me know.

thanks for any advise

public Password(String password)
    {
        // initialise instance variables
        password = userPassword;

    }

You want these the other way around. This line should assign your parameter (password) to the instance field (userPassword) so the rest of the class can use it.


Your isValid method looks troublesome to me. For one thing, you have a static method trying to use instance data (isValid is marked as static, userPassword is an instance variable). This won't compile, because a static method can't see instance data. You could have this as a static method, but you'd have to pass in all of the data you need as parameters or else declare it as static variables (which you don't want, presumably, for a password class, since you want to have more than one password in the world).
For another thing, using exceptions this way is problematic. I would think you'd want the method to return true if the password is valid, false if not. What you have so far is it returns false if the password is valid (by the tests you've implemented so far) and throws an exception if it's invalid. Exceptions are for exceptional conditions which the program should handle. By definition, the false condition of a boolean should not be exceptional.

Oh, and your instance fields (like userPassword) should be private. This isn't very important for your current aplication, but you should just get in the habit of always making fields private.


So there are some things to tidy up here.

I'd suggest you try to address these things so that your isValid method returns false for the conditions you've checked, and true otherwise. Once you do that, it'll be relatively easy to test the other conditions.

commented: Very important information Great Job +1

Thanks so much for your imput. I will work on cleaning that up. My instructions are to make the method isValid static and that's where I'm running into problems. I will try your suggestions and continue on.
thank you
carolyn

If the method has to be static, it can't refer to private instance fields, for the same reason instance Q can't refer to instance R's private instance fields - they're private. The class isn't any instance of itself, so it can't access any of its instances' private data, any more than they can access each others'.

It can get at static fields, and it can use stuff that you hand it as a parameter. In this case, you'll want to pass the password to validate as a parameter - again, the other option is for there to be only one password in the world.

So the method signature would be

public static boolean isValid(String password)

That method will be able to verify any arbitrary password. hope that helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.