Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: This program works but Want to improve

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Location
    Lolo, Mt
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default This program works but Want to improve

    my code is a very basic password generator i was wondering if i could do something to blow my computer science teachers mind away. this code only took about 25 minutes to right up i built the base and was looking for great ideas on what i could do

     
    // The "PasswordGenerator" class.
    import hsa.Stdin;
    import java.util.Random;
    public class PasswordGenerator
    {
        public static void main (String[] args)
        {
            //declaring variables
            boolean end = false;
            String passLetters = "ABCDWFGHIJKLMNOPQRSTUVWXYZ";
            int numOfPassLetters, numOfPassNumbers, numOfPasswordsGenerated;
            Random myRandom = new Random ();
     
            System.out.println ("RANDOM PASSWORD GENERATOR\n");
            do
            {
                System.out.print ("\nEnter the number of letters you want in your pasword: ");
                numOfPassLetters = Stdin.readInt ();
                System.out.print ("\nEnter the number of numbers you want in your password: ");
                numOfPassNumbers = Stdin.readInt ();
                System.out.print ("\nEnter How many Passwords that you want to display to choose from: ");
                numOfPasswordsGenerated = Stdin.readInt ();
                // number of passwords Generated 
                for (int numOfPasswords = 0 ; numOfPasswords < numOfPasswordsGenerated ; numOfPasswords++) 
                {   //number of letter in each password
                    for (int numOfLetters = 0 ; numOfLetters < numOfPassLetters ; numOfLetters++)
                    {
                        int letterSelecter = myRandom.nextInt (25) + 1;
                        System.out.print (passLetters.substring (letterSelecter, letterSelecter + 1));
                    }//numbr of numbers placed in each password
                    for (int numOfNumbers = 0 ; numOfNumbers < numOfPassNumbers ; numOfNumbers++)
                    {
                        int numberSelector = myRandom.nextInt (9);
                        System.out.print (numberSelector);
                    }
                    System.out.print ("\n\n");
                }
                System.out.print ("Would you like to generate more passwords? type 1 to generate more\nEnter 2 to stop: ");
                int determineEnd = Stdin.readInt ();
                if (determineEnd == 2)
                {
                    System.out.println ("\nProgram will now Terminate");
                    end = true;
                }
            }
            while (end == false);
     
     
        } // main method
    } // PasswordGenerator class
    Attached Files Attached Files
    Last edited by helloworld922; January 8th, 2011 at 12:11 AM.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: This program works but Want to improve

    // The "PasswordGenerator" class.
    import hsa.Stdin;
    import java.util.Random;
    public class PasswordGenerator
    {
    public static void main (String[] args)
    {
    //declaring variables
    boolean end = false;
    String passLetters = "ABCDWFGHIJKLMNOPQRSTUVWXYZ";
    int numOfPassLetters, numOfPassNumbers, numOfPasswordsGenerated;
    Random myRandom = new Random ();
     
    System.out.println ("RANDOM PASSWORD GENERATOR\n");
    do
    {
    System.out.print ("\nEnter the number of letters you want in your pasword: ");
    numOfPassLetters = Stdin.readInt ();
    System.out.print ("\nEnter the number of numbers you want in your password: ");
    numOfPassNumbers = Stdin.readInt ();
    System.out.print ("\nEnter How many Passwords that you want to display to choose from: ");
    numOfPasswordsGenerated = Stdin.readInt ();
    // number of passwords Generated
    for (int numOfPasswords = 0 ; numOfPasswords < numOfPasswordsGenerated ; numOfPasswords++)
    { //number of letter in each password
    for (int numOfLetters = 0 ; numOfLetters < numOfPassLetters ; numOfLetters++)
    {
    int letterSelecter = myRandom.nextInt (25) + 1;
    System.out.print (passLetters.substring (letterSelecter, letterSelecter + 1));
    }//numbr of numbers placed in each password
    for (int numOfNumbers = 0 ; numOfNumbers < numOfPassNumbers ; numOfNumbers++)
    {
    int numberSelector = myRandom.nextInt (9);
    System.out.print (numberSelector);
    }
    System.out.print ("\n\n");
    }
    System.out.print ("Would you like to generate more passwords? type 1 to generate more\nEnter 2 to stop: ");
    int determineEnd = Stdin.readInt ();
    if (determineEnd == 2)
    {
    System.out.println ("\nProgram will now Terminate");
    end = true;
    }
    }
    while (end == false);
     
     
    } // main method
    } // PasswordGenerator class

    It might be nice to have that other package, which I've never heard of before, posted here too.

    Also, I must tell you that the three or so ints your reading in should be initialized.

    I've often tried stuff like this and gotten the message

    "variable whatever might not have been initialized."

    It's a common mistake that many people, myself included, often make.

  3. #3
    Member
    Join Date
    Dec 2010
    Posts
    46
    Thanks
    0
    Thanked 10 Times in 10 Posts

    Default Re: This program works but Want to improve

    i would rather use Scanner class for your input. Also, you could probably do a shuffle() method (or use the shuffle() method from Collections class) so that your numbers and letters are jumbled up instead of being predictable
    Last edited by JavaHater; January 7th, 2011 at 10:59 PM.

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: This program works but Want to improve

    Or you could have it choose randomly from all the letters, lowercase, uppercase, and numbers for a certain length.

  5. #5
    Member
    Join Date
    Dec 2010
    Posts
    46
    Thanks
    0
    Thanked 10 Times in 10 Posts

    Default Re: This program works but Want to improve

    Quote Originally Posted by javapenguin View Post
    Or you could have it choose randomly from all the letters, lowercase, uppercase, and numbers for a certain length.
    the requirement should have digits and letters ( as in OP's post). what if the result of randomly choosing from all letters(upper+lower) and numbers ended up in all letters? (or all digits? ).

  6. #6
    Junior Member
    Join Date
    Jan 2011
    Location
    Lolo, Mt
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: This program works but Want to improve

    Quote Originally Posted by JavaHater View Post
    the requirement should have digits and letters ( as in OP's post). what if the result of randomly choosing from all letters(upper+lower) and numbers ended up in all letters? (or all digits? ).
    i was thinking about doing such maybe giving the user the choice for all lower upper or mixed it would be an improvement, and i have used scanner before in a rock paper scissors game i was thinking something along the lines that dealt with strings because that is what im finishing up this class with i'll make the changes and repost

  7. #7
    Junior Member
    Join Date
    Jan 2011
    Location
    Lolo, Mt
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: This program works but Want to improve

    I atempted to do what was asked and incountered a few errors can anyone tell me how to fix this so it works?
     
    /*
    * im trying to do this to allow mulitiple choices it wont tske it a i dont know how to fic it
                    if (passSize == 1)
                    {
                         letterSelecter = myRandom.nextInt (25) + 1;
                        passLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                    }
                    if (passSize == 2)
                    {
                         letterSelecter = myRandom.nextInt (25) + 1;
                        passLetters = "abcdefghijklmnopqrstuvwxyz";
                    }
                    if (passSize == 3)
                    {
                         letterSelecter = myRandom.nextInt (51) + 1;
                        passLetters = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";
                    }
    */
    // The "PasswordGenerator" class.
    import hsa.Stdin;
    import java.util.Random;
    public class PasswordGenerator
    {
        public static void main (String[] args)
        {
            //declaring variables
            boolean end = false;
            String passLetters;
            int numOfPassLetters, numOfPassNumbers, numOfPasswordsGenerated,letterSelecter;
            Random myRandom = new Random ();
     
            System.out.println ("RANDOM PASSWORD GENERATOR\n");
            do
            {
                System.out.print ("\nEnter 1 for all caps, 2 for all lower cased and 3 for both");
                int passSize = Stdin.readInt ();
     
                System.out.print ("\nEnter the number of letters you want in your pasword: ");
                numOfPassLetters = Stdin.readInt ();
                System.out.print ("\nEnter the number of numbers you want in your password: ");
                numOfPassNumbers = Stdin.readInt ();
                System.out.print ("\nEnter How many Passwords that you want to display to choose from: ");
                numOfPasswordsGenerated = Stdin.readInt ();
                // number of passwords Generated
                for (int numOfPasswords = 0 ; numOfPasswords < numOfPasswordsGenerated ; numOfPasswords++)
                { //number of letter in each password
     
                    for (int numOfLetters = 0 ; numOfLetters < numOfPassLetters ; numOfLetters++)
                    {
                        System.out.print (passLetters.substring (letterSelecter, letterSelecter + 1));
                    } //numbr of numbers placed in each password
                    for (int numOfNumbers = 0 ; numOfNumbers < numOfPassNumbers ; numOfNumbers++)
                    {
                        int numberSelector = myRandom.nextInt (9);
                        System.out.print (numberSelector);
                    }
                    System.out.print ("\n\n");
                }
                System.out.print ("Would you like to generate more passwords? type 1 to generate more\nEnter 2 to stop: ");
                int determineEnd = Stdin.readInt ();
                if (determineEnd == 2)
                {
                    System.out.println ("\nProgram will now Terminate");
                    end = true;
                }
            }
            while (end == false);
     
     
        } // main method
    } // PasswordGenerator class
    there is what im at this point im thinking of allowing the option of mixing letters and numbers but havent got there yet i need to get past this stage before i make any more changes
    Last edited by SHStudent21; January 8th, 2011 at 06:53 PM.

Similar Threads

  1. [SOLVED] Can someone verify if this code for deleting a BST works?
    By scottb80 in forum Java Theory & Questions
    Replies: 2
    Last Post: November 2nd, 2010, 10:19 AM
  2. Critique Java Game: Help Me Improve
    By gretty in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 21st, 2010, 07:49 PM
  3. Works on debug mode but not on run mode
    By alfonsoraul in forum Member Introductions
    Replies: 0
    Last Post: April 14th, 2010, 02:58 PM
  4. How overloaded paint() works?
    By maikeru in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 21st, 2009, 06:13 PM
  5. How database connection pooling works in a application
    By JayVirk in forum JDBC & Databases
    Replies: 0
    Last Post: October 10th, 2009, 07:14 AM

Tags for this Thread