4

I have an exercise about the analysis of passwords (weak vs strong) and I have to create 2 constructors. One of them must receive the length of a password and the password must be created randomly, how can I do that ?

Also I'm having trouble determining if the password is weak or strong. It's going to be strong if the password has at least 3 uppercase letters, 2 lowercase letters and 6 numbers.

class Password {
    private double length;
    private String password;

    Password()//default constructor with password length 8
    { 
        password="defaultt";
        length=8;
    }

    Password(double length)
    {
        this.length=length;
        password="";//generate a random password, how?
    }

    boolean isStrong(String password)
    { 
        int totalnumbers=0;
        int upper=0; 
        int lower=0;

        for(int i=0;i<password.length;i++)//marks an error in .length why?
        {
            if (password.charAt(i)>='0' && password.charAt(i)<='9')
                totalnumbers+=1;

            if (password.charAt(i) is lowercase)//what can I do here?
                lower+=1;

            if (password.charAt(i) is uppercase)
                upper+=1;
        }

        if(totalnumbers>=6 && lower>=2 && upper>=3)       
            return true; 
        else
            return false;

    }

    generatePassword(double length)
    {
        password;//generate the password of the object with this.length                 //how to do it?
    }

    String getPassword()
    {
        return password;
    }

    double getLength()
    {
        return length;
    }

    void setLength(double length)        
    {
        this.length=length;
    }

}


public class Exercises2 {

    public static void main(String[] args) {
        Password x=new Password();
        Password array[];
        Scanner rd=new Scanner(System.in);
        int Size;

        System.out.println("Give the size of the passwords array ");
        Size=rd.nextInt();
        array=new Password[Size];

        for( int i=0;i<Size;i++)
        {

            array[i]=new Password();//creation of an object for each position of the array
        }

        for(int j=0;j<Size;j++)
        {
            System.out.println("Give the size for each password of the array ");   
            array[j]=[rd.nextInt]; //length for each password of the array
        }

        Password arrayBooleans[]=new Password[Size];
        for (int k=0;k<Size;k++){
            arrayBooleans[k]=x.isStrong(array[k]);

        }

        for(int i=0;i<Size;i++)
        {
            System.out.println("password "+i+ arrayBooleans[i]);
        }   

    }
}
Jason
  • 11,744
  • 3
  • 42
  • 46

3 Answers3

3

First, you shouldn't use a double for the number of characters in the password. How could you have 1.5 characters? Just use an int.

To check if a character is a digit, lower case or upper case, use the Character class (don't rely on ASCII values):

for (int i = 0; i < password.length(); i++) {

    Character c = Character.valueOf(password.charAt(i));
    if (c.isDigit()) {
        totalnumbers++;
    }

    if (c.isLowerCase()) {
        lower++;
    }

    if (c.isUpperCase()) {
        upper++;
    }
}

To generate a random alphanumeric string of a given length, take a look at the accepted answer for this SO post. There are many different ways to do it, but this is as good a place as any to start.

Jason
  • 11,744
  • 3
  • 42
  • 46
0

You have a few different questions here, so I'll do my best to answer them all:

Random password:

This SO question talks about random password generation, so for more details, look into what is posted there, but essentially you need to use some random generation that is built into Java and create a string out of it. There are many different ways to do it, which is detailed in that question, so I'll leave that to you to decide how you wish to do it.

Error in for loop:

Calling password.length tries to access a variable called length in the password object. In Java, you can't do that, you must use the method:

password.length()

Uppercase/lowercase detection

This SO question shows a good way to detect if a character is uppercase or lowercase based on its char value. Snippet:

if(input>='a'&&input<='z')
    //lowercase

if(input>='A'&&input<='Z')
    //uppercase
captainGeech
  • 302
  • 1
  • 5
  • 17
-1

Try to use the simple logic,

Use Password()
{
// Simply check length
if(length == 8)
{
// Call a method which would check that password is strong or weak. for e.g, 
}
}

// You can use the class java.util.Random with method to generate the password for e.g,

 char c = (char)(rnd.nextInt(128-32))+32 
 20x to get Bytes, which you interpret as ASCII. // If you're fine with ASCII.
// Call a method which would check that password is strong or weak.

Use the given link for random generation of password:

Creating a random string with A-Z and 0-9 in Java

http://www.baeldung.com/java-random-string

Tehmina
  • 205
  • 2
  • 8
  • I saw a code for random generation password (in your first link), but I don't know how to call the returned random password to asiggn password (in the constructor). How can I do that? –  Aug 11 '17 at 04:00
  • 1
    You just call method in the constructor. There would be 2 methods which will use in your program. 1st Method checks password is strong or weak. if it is strong you just simply print it. Your 2nd method generate random password and return it to your 1st method for checking. You just simply call the methods in your constructor. – Tehmina Aug 11 '17 at 04:07
  • I did: `Password(int length) { Password x=new Password(); this.length=length; password=x.generatePassword(length); }` however when I call this constructor from the main class an error appear:`System.out.println("this is a random password of length: "+x.Password(4));` Am I doing something wrong? –  Aug 11 '17 at 04:30
  • 1
    public class Main { public static void main(String[] args) { Password c= new Password(4); } public void generatePassword() { // generate password here } } class Password { Password(int l) { System.out.println(l); } } – Tehmina Aug 11 '17 at 04:50