|
-
May 19th, 2011, 03:10 PM
#1
Help
I'm relatively new to programming, and recently I've been working on a random password generator. The problem I'm having is that, for some reason I can't figure out, the passwords generated aren't the right size or the right characters(In fact, all it produces is a password full of numbers).
Code:
/*
* This program is used to generate a password using both lower case and upper case
* letters, numbers, and symbols
*
* Currently, this program kinda works. It does not create a password with the right number of characters
* or the right characters
*/
package passwordgenerator;
import java.util.*;
import java.io.*;
public class Main
{
public static void main(String[] args) throws IOException
{
File file = new File("Passwords.txt");
BufferedWriter output = new BufferedWriter(new FileWriter(file));
Scanner scan = new Scanner(System.in);
Random generator = new Random();
String string;
int passSize = 0; //The size of the password
//The array that contains the possible characters for the password(lower case/upper case/special characters/numbers)
int[] lArray = {'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o'
, 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F',
'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1',
'2','3','4','5','6','7','8','9','0', '!', '@', '#', '$', '%', '+', '-' };
System.out.println(lArray.length-1); //Not sure why I have this. Probably a test output to get the last index of lArray
System.out.println("Size of password: ");
passSize = scan.nextInt();
int[] pArray = new int[passSize]; //The array that will contain the password
//This for loop creates the password
for(int i = 0; i < passSize; i++)
{
pArray[i] = lArray[generator.nextInt(lArray.length)];
}
//System.out.println("Website/Program password is for"); //This was meant to add the name of what the password was for.
//Currently not working
string = scan.nextLine() + "";
for(int i = 0; i < pArray.length; i++)
{
string += pArray[i];
}
output.write(string);
output.close();
System.out.println("Password generated. Saved in Passwords.txt");
}
}
I think it has something to do with the first for loop, but I'm not sure what
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|