Results 1 to 5 of 5
  1. #1
    Basit56 is offline Member
    Join Date
    Aug 2009
    Posts
    48
    Rep Power
    0

    Default How to generate 8 character long alphanumeric string

    Hi,
    Hope you all will be fine.I want to generate 8 character long alphanumeric string and for this i used this line of code

    Java Code:
     System.out.println(Long.toHexString(Double.doubleToLongBits(Math.random())));
    But the problem is this gives me 16 character long string how can i reduce it to 8 characters, please help

    Thank you

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    But you won't get all the alphanumerics with that.
    Better have a look at
    Java Code:
                    String alphaNumerics = "qwertyuiopasdfghjklzxcvbnm1234567890";
    		String t = "";
    		for (int i = 0; i < 8; i++) {
    			t += alphaNumerics.charAt((int) (Math.random() * alphaNumerics.length()));
    		}
    		System.out.println(t);

  3. #3
    Basit56 is offline Member
    Join Date
    Aug 2009
    Posts
    48
    Rep Power
    0

    Default

    Hi :),
    Thank you very much. You post a nice code because it's very very simple and up to my expectations, but i found this code, it's also very easy. I am posting it here so others can also look at it if they want to like it.

    Java Code:
    public class AlphaNumeric {
        
        private static final String ALPHA_NUM ="0123456789abcdef";
    
        public static String getAlphaNumeric(int len) {
            StringBuffer sb = new StringBuffer(len);
            for (int i=0; i<len; i++) {
                int ndx = (int)(Math.random()*ALPHA_NUM.length());
                sb.append(ALPHA_NUM.charAt(ndx));
            }
            return sb.toString();
            }
    
        public static void main(String[] args) {
                
            System.out.println(" character long: " +getAlphaNumeric(8));
          
            /** 
             * Another method
            String alphaNumerics = "qwertyuiopasdfghjklzxcvbnm1234567890";
            String t = "";
            for (int i = 0; i < 8; i++) {
                t += alphaNumerics.charAt((int) (Math.random() * alphaNumerics.length()));
            }
            System.out.println("with new code: " + t);
             */
        }
    }
    Thank you:)

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    If you want to use a buffer for the result then use StringBuilder instead of StringBuffer.
    StringBuilder is faster.

  5. #5
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

Similar Threads

  1. Replies: 7
    Last Post: 10-12-2009, 11:16 PM
  2. Generate a random code 4 letters long
    By bl00dr3d in forum New To Java
    Replies: 9
    Last Post: 04-06-2009, 05:32 AM
  3. how to get next character/string
    By doha786 in forum New To Java
    Replies: 3
    Last Post: 03-28-2009, 04:04 AM
  4. Replies: 0
    Last Post: 02-07-2008, 08:57 AM
  5. Error: convert from String to long
    By bbq in forum New To Java
    Replies: 1
    Last Post: 06-29-2007, 07:23 PM

Posting Permissions

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