Help
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Help

  1. #1
    Join Date
    May 2011
    Posts
    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

  2. #2
    Join Date
    Jun 1999
    Location
    SW Missouri
    Posts
    3,450

    Re: Help

    Try debugging the code by printing out the value returned by nextInt() before using it as an index to the array of chars. Put the value into a variable, print it then use it.

    Also look at the data types you are using. The array of letters are ints not chars.
    Also look at the String class to see how to convert an array of char to a String.
    Last edited by Norm; May 19th, 2011 at 05:55 PM.
    Norm

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center