Hey guys, new to the forum. So sorry if i am posting this in the wrong location.

To my question:

I am trying to create a random password generator, and I think it's
done is this way. I have made four arrays, two for the alphabet (capital letters and not), one for 50 different numbers(or more) and one for miscellaneous signs.

This is about how far I've come, and i need your help with the rest of this, my guess would be that i need to 'draw' say four letters, one number and one sign. The user is supposed to be able to enter the length of the password himself. But I might be able to solve that on my own.

Thanks in advance.

package pass;
import java.util.*;

public class Main {


    public static void main(String[] args) {
        
        print();

    }

    public static void print(){
        
        list();
        System.out.println("");
        System.out.println(misc());        
        System.out.println(Alfa1());
        System.out.println(Alfa2());
        
    }
    
    public static int[] list(){
        
        int nList1 [] = new int [50];
        
        for(int i = 0; i < nList1.length; i ++)
        {           
            nList1[i] = i+1;           
        }
                
        for(int i = 0; i < nList1.length; i++)
        {
            System.out.print(nList1[i] + " ");
        }
        
        return nList1;
        
    }    
 
    public static char[] misc(){
        
        char cList1 [] = new char [14];
        
        int nMisc = 32;
        
        for(int i = 0; i < cList1.length; i ++)
        {
            nMisc++;
            cList1[i] = (char) nMisc;
        }
        
        return cList1;
    }
 
    public static char[] Alfa1()
    {
        char cAlfa1 [] = new char [26];
        int letters = 63;

        for(int i = 0; i < cAlfa1.length; i ++)
        {
        letters++;
        cAlfa1[i] = (char) (letters+1);
        }
        return cAlfa1;
    }
    
    public static char[] Alfa2()
    {
        char cAlfa2 [] = new char [26];

        int letters = 96;
        
        
        for(int i = 0; i < cAlfa2.length; i++)
        {
            letters++;
            cAlfa2[i] = (char) letters;
        }
        return cAlfa2;
    }

}

Edited 6 Years Ago by Fulsomenko: n/a

I would have put all characters (upper and lower cases) that I would like to use in the same collection, unlike you put it in four different. Take it size and run random generator on collection size number of times declared by me or user.

I would have put all characters (upper and lower cases) that I would like to use in the same collection, unlike you put it in four different. Take it size and run random generator on collection size number of times declared by me or user.

That's good if you want a really random string with minimum code, but if the requirement is to have (eg) at least one lower case, at least 1 upper case, at least 1 number (etc), and other chars may or may not be acceptable, then the four arrays may not be such a bad idea.

Have a look at the java.util.Random class in the API. You can use this to select random letters etc from your arrays.

Sorry guys, i know I'm not the best programmer in the world. But i stared at the Random() API but it told me nothing, do I need to create a Random() method to draw my numbers/chars from my arrays how do i tell my main method that i want to use the arrays and take a sample of chars and mix them up?

I lost my train of thought.

Oh well class is starting.

I'll be back...

There are plenty of example/tutorials on the internet, just use Google ;), just like this one

Yeah but the ones I've found and the one you linked is all generating random integer values, and i don't know how to bring that in to my program.

If you have an array of (say) 26 letters you can generate a random integer between 0 and 25, then use that as an index into the array, thus selecting a random letter. Do that in a loop to generate a sequence of random letters ...

If you have an array of (say) 26 letters you can generate a random integer between 0 and 25, then use that as an index into the array, thus selecting a random letter. Do that in a loop to generate a sequence of random letters ...

You just made my day.

Sorry if I undermined what you were trying to do. It seemed to me that the O/P was still completely stuck, so I tried to give a bit more of a steer without actually providing a detailed solution or actual code (or solutions to the next set of questions (and we both know what they will be!)). I am a complete believer in giving just enough info to unstick the problem while leaving the maximum possible scope for the poster to do their own research and learning. Cheers. J

No worries James I'm not complaining. Just saying that may answer was done the way not give out all, try to push OP to think about and either solve it or take further action on it. I do not see anything wrong with you giving him hint after the specific question was posted.

This question has already been answered. Start a new discussion instead.