mardi 24 février 2015

Program that creates a random string of characters and acts as a password generator

I'm writing this program that asks the user for password length and then creates a random string of characters that length using characters ! to ~. The only part that I am stuck on is that it is creating multiple strings where I only need one. Any input would be appreciated.



import java.util.*;
public class pw
{
public static void main(String[]arg)
{
Scanner in = new Scanner(System.in);
Random gen = new Random();
System.out.println("Enter password length: ");
int length = in.nextInt();

for(int i=1; i < length; i++)
{
int slength = length+gen.nextInt(length - 1);
StringBuilder s = new StringBuilder(slength);

while(s.length() <= length)
{
s.append((char)(33+gen.nextInt(126)));
}
System.out.println(s.toString());
}
}
}




Aucun commentaire:

Enregistrer un commentaire