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 4 of 4

Thread: Creating a 8 digit password mixture of numbers and letters

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Creating a 8 digit password mixture of numbers and letters

    What i want to do is generate a random 8 digit password consisting of 8 characters.

    The possible characters would be 0-9 and a-z.

    Here is what i have so far but i will warn you i think i am going down the copmlete wrong road with this one:

    package latesttasks;
     
    // Password Generator
    // 8 characters, can be; 0-9 and a-z
     
    import java.util.Random;
     
    public class Task6 {
     
        public static void main(String[] args) {
     
            // The 8 Random Numbers 0 to 35 
            int r1, r2, r3, r4, r5, r6, r7, r8;
            // The 8 individual characters
            String c1, c2, c3, c4, c5, c6, c7, c8;
            // The complete password - the 8 strings joined
            String complete;
            // The complete library of possible characters
            String[] characters = new String[]{"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", "0", 
                "1", "2", "3", "4", "5", "6", "7", "8", "9"};
     
            Random rand = new Random();
     
            r1 = rand.nextInt(36);
            r2 = rand.nextInt(36);
            r3 = rand.nextInt(36);
            r4 = rand.nextInt(36);
            r5 = rand.nextInt(36);
            r6 = rand.nextInt(36);
            r7 = rand.nextInt(36);
            r8 = rand.nextInt(36);
     
     
        }
    }


    ............ What my plan was was to create a String array to hold each individual character 0-9 and a-z.

    Then create 8 random numbers using Random(36) (which is a random number 0 to 35)

    Each of the random numbers would be used as the index to get a character from the array of characters i created. For example if the random number generated is 34, then that character would be the 34th index of the array.


    So what I am stuck on is assigning the index's of the array to variables: c1, c2, c3, c4, c5, c6, c7, c8 Those variables represent character 1, character 2 etc.

    I was hoping i could of used something like this:

    c1 = characters.charAt(r1)

    where r1 is the random number generated to be used as the index number ?


    I am really stuck on this.

    What kind of Array do i need and what methods do i need to assign the random index numbers of the array to a new variable.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Creating a 8 digit password mixture of numbers and letters

    You index into an array and get a reference to one of its slots by using array notation: theArray[theIndex]
    That reference to a slot can be used anywhere a variable is used.

    Arrays do not have methods that can be called: characters.charAt(r1)
    that statement looks like it is trying to call the method charAt() using the object reference characters.

    When you get compiler errors, you should copy and paste here the full text of the error message.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Creating a 8 digit password mixture of numbers and letters

    Quote Originally Posted by Norm View Post
    You index into an array and get a reference to one of its slots by using array notation: theArray[theIndex]
    That reference to a slot can be used anywhere a variable is used.

    Arrays do not have methods that can be called: characters.charAt(r1)
    that statement looks like it is trying to call the method charAt() using the object reference characters.

    When you get compiler errors, you should copy and paste here the full text of the error message.
    Thanks so for the first character it would be:

    c1 = characters[r1];

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Creating a 8 digit password mixture of numbers and letters

    That looks ok. Compile and execute it and see what ends up in c1 by printing c1.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Adding 100 single digit numbers to a Text Field
    By CodyReuille in forum Java Theory & Questions
    Replies: 2
    Last Post: July 3rd, 2012, 03:58 PM
  2. Replies: 13
    Last Post: December 6th, 2011, 02:17 PM
  3. How to generate 13 digit numbers to text file
    By duanedtp in forum Java Theory & Questions
    Replies: 3
    Last Post: April 27th, 2011, 11:30 AM
  4. Generating random letters and numbers together
    By newJava in forum Java Theory & Questions
    Replies: 3
    Last Post: March 19th, 2010, 04:08 AM
  5. help me .. about creating random numbers
    By soldier in forum Java Theory & Questions
    Replies: 2
    Last Post: December 20th, 2009, 08:24 PM