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: Java password generator program to generate 8 random integers

  1. #1
    Junior Member
    Join Date
    May 2009
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question Java password generator program to generate 8 random integers

    So I'm trying to create a password generator that generates 8 random integers.

    48-57
    65-90
    97-122

    so basically any letter uppercase or lower case and numbers.

    I think I'm on the right track but I could really use some help. Please show me how this could be done.

    import java.util.Random;
    import java.util.*;
     
    public final class PasswordGenerator {
        public static final void main(String[] args) {
     
    int num, counter, randomNum;
    counter = 0;
    String password = "";
     
    while (counter < 8)
    {
      randomNum = (int)(Math.random()*100);
      if (randomNum => 65) 
        num = Random.range(97,122);
     
         else if...
    Last edited by Deep_4; November 7th, 2012 at 10:48 PM.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Password Generator?

    Hello Lizard,

    I'm not sure what you are trying to do here?

    Please give me an example output.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    May 2009
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Password Generator?

    aqT6wWT

    any combination of letters (uppercase) (lowercase) and numbers. The password should only 8 in length.

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Password Generator?

    Afternoon Lizard,

    How about this code?

    It's completely random. The generated password contains numbers from 0 to 9, keyboard charactor including lower & upper case A-Z.

    public class PasswordGenerator {
     
        /**
         * JavaProgrammingForums.com
         */
        public static int count, random, intOrChar, upperOrLower;
        public static char myChar;
        public static String lowerCase, password, finalPasswd;
     
        public static void main(String[] args) {
     
            while (count < 8) {
     
                // Integer or character
                intOrChar = (int) (Math.random() * 2);
     
                // if 0 then Integer
                if (intOrChar == 0) {
     
                    // Print random number 0-9
                    random = (int) (Math.random() * 9);
                    //System.out.print(random);
                    password = Integer.toString(random) + password;
     
                    // if 1 then character
                } else if (intOrChar == 1) {
     
                    // decide lower or upper case
                    upperOrLower = (int) (Math.random() * 2);
     
                    // if 0 then upper case
                    if (upperOrLower == 0) {
     
                        // Print random Capital char
                        random = (int) (Math.random() * 74);
                        myChar = (char) (random + '0');
                        //System.out.print(myChar);
                        password = Character.toString(myChar) + password;
     
                        // if 1 then lower case
                    } else if (upperOrLower == 1) {
     
                        // Print random Small char
                        random = (int) (Math.random() * 74);
                        myChar = (char) (random + '0');
                        lowerCase = Character.toString(myChar).toLowerCase() + password;
                        //System.out.print(lowerCase);
                        password = lowerCase;
     
                    }
     
                }
     
                count++;
            }
            finalPasswd = password.replace("null", "");
            System.out.println(finalPasswd);
        }
     
    }
    Example output:

    e05A34z5
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. [SOLVED] Random number method implementation
    By big_c in forum Java Theory & Questions
    Replies: 2
    Last Post: April 15th, 2009, 01:10 PM