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

Thread: Have I done this efficiently ?

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Have I done this efficiently ?

    So sorry if this is in the wrong section, I am new to this forums

    So guys i just quickly made this, can y'all tell me if its alright, like if it is effiiant, if not can you explain how I can make this really efficient, please note this is not a 'serial' key generator to unlock software or anything

    import java.util.Random;
     
    /**
     * This class is used to auto generate random codes
     * Made this just as an experiment
     * @author Zack
     *
     */
     
    public class RandomCodeGenerator {
     
    	/**
    	 * This is all the Keys that can be made in the code generator
    	 */
     
    	private static String[] keys = {
    			"1", "2", "3", "4", "5", "5", "6", "7", "8", "9",
    			"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"
    	};
     
    	/**
    	 * This is the String for the code
    	 */
     
    	static StringBuilder sb = new StringBuilder();
     
    	/**
    	 * This variable determains how much codes you generate
    	 */
     
    	public final static int REPEAT = 10000;
     
    	/**
    	 * This variable counts how much characters will be in the code itself
    	 */
     
    	public final static int CHARACTERS = 25;
     
    	/**
    	 * This variable places a dash after a certain amount of characrtes, so for instance
    	 * if it is set to 5 it will put a dash after every 5 characters
    	 */
     
    	public final static int DASH = 5;
     
    	/**
    	 * This will generate a randomized code and return the value
    	 * @return StringBuilders value, all the Strings that have been appended
    	 */
     
    	public static String generateCode(){
    		Random r = new Random();
    		int counter = 0;
    		int dashCounter = 0;
    		sb.delete(0, CHARACTERS);
    		for(int i = 1; i <= CHARACTERS; i++){
    			sb.append(keys[r.nextInt(keys.length - 1)]);
    			counter++;
    			if(counter == DASH){
    				dashCounter++;
    				if(dashCounter == CHARACTERS / DASH){ } else {
    					sb.append("-");
    					counter = 0;
    				}
    			}
    		}
    		return sb.toString();
    	}
     
    	/**
    	 * This is the main method that will repeat the code generator
    	 * @param args
    	 */
     
    	public static void main(String[] args) {
    			for(int i = 0; i < REPEAT; i++){
    			System.out.println(generateCode());
    			sb.delete(0, CHARACTERS);
    		}
    	}
    }

    here is an example how the codes look

    BQ935-14CKK-64Z8N-PLESN-HHBDZ
    5H45P-MEC9H-NM2TA-FF22S-XQX6L
    P7BXD-FUM21-JRY5R-GVNEM-KIU4B
    W5WTI-AKYEM-NO7EN-UUWG4-5NO7R
    XUB43-3BZTY-K3FBK-Z9HDS-PHUDF
    RDTUA-X9USV-CUPWN-8Q92J-IOOVC
    YJS3E-D5GJS-3COPL-SLU33-TB8SU
    UGHI1-7KW1O-XPLTN-S4DEF-5USVE
    TP4GI-C6OE5-CI8CM-4BAC2-M88BU
    RK7RZ-BGK1V-WZSK7-R8Q52-NFM3A
    8CKWM-8HTCV-YWSJM-LCKRT-KX6ZB
    16BVU-7NQMG-55FCQ-K33DZ-62Z38
    WL1AT-PD595-IKVIX-C4OIE-4BKRG
    66ZCO-FNY9B-OYY1R-VNI87-JENI3
    OSJ6D-A5K2W-ZBEW9-SXSOR-IPJWW
    GJ53Q-UJ6TL-54VY9-3SP4J-1UR9F
    26S23-CYL6A-7NZ75-7JUOE-4NUBC
    JASCT-I5CYA-15AKV-72Y7X-CK3BQ
    VGS5X-YQFBY-D16NL-P35AI-ECMWU
    CZ1H5-859P7-WPD1Z-JFWOE-YF3ZQ
    NUOML-2RFJY-UCYOX-AOTPW-5GTZ8
    AU43B-DJLHR-96UZ5-ZQANL-XYO9N
    YFING-5RRP3-N3UK8-2L25U-N8N2F
    W8BGS-7DUI3-3K5GZ-1WSCB-D4CPT
    5SB5E-D34HA-Q571X-LEZUE-9J53C
    TDDE3-39XVU-Y9U22-GMQFB-FBMCZ
    M6WMI-4IEPJ-BUPD3-GIM7D-2ZX8P
    GNN2Y-RN28W-GVG3W-4FHN5-IUVIM
    A1QOU-Y7YW5-R8AJL-3SJ9O-X5F4W
    65K8F-CC7I9-LUN63-ENRXW-BX9RN
    SVYAV-S2T3E-ETYES-BVHBS-IQ9X9
    Q3DO3-9M944-6NG3N-3I7QM-LWJQ3
    GXOLX-5QIQF-Y75QB-HIAOM-KQO55
    NJPNG-85TNK-XR5P9-7FN2U-5OB7O
    QP6NN-5CWAR-R19QV-RX439-PJEY9
    YYKL9-W8QBW-C5V53-SJ5UZ-P1PIW
    83JXA-HWU2I-S9W9C-X86UF-DIFCU
    6NFM2-ZHBSN-ZQ9K3-G5D6K-8NVU9
    YZLBD-V8ESU-NMQCL-IX5D5-KIV55
    4USEE-DXMPV-6AICK-MP5I5-NK1O8
    UTCWJ-66KV2-87KL9-DDY5R-HAP49
    G1Q9M-SWGEF-IB5ID-C37ZI-SWXT5
    D46XW-7DL5W-JG8FN-ZQXH3-9BGPP
    DAJMM-GN7R9-TD9J9-FHVRT-Y6P15
    12WFS-NE8RT-MZ44E-4CY8W-77Z4A
    Q1MAU-M29KP-9UKNW-PE5FK-XX3DZ
    YJ2YI-9M8F9-7GKRK-RMN9P-ZX3WU
    W55BV-M3FA5-I44U8-Z5UXP-TCLBF
    ZR1ZK-5GTN1-W1OI5-UI55C-W1X5U
    Z8ZA9-CPZPE-3LNL9-CBFF1-7EUOM
    ETMZI-YRZHS-Z3T7T-89U7E-VGEJ3
    4HI87-LPNRV-KGOK8-W4KB4-VV7LD
    G25DK-JDOBJ-55WK6-Q3E9P-DUASU
    LSTNG-9Z239-TUERG-698WA-WVNUP
    2SGKB-7IB7C-6PXI2-DSJSD-5QMDR
    85XB2-32D9G-5B5MC-PGX3F-J456N
    RGN4Q-ZPSGV-6KY8M-PUK6O-G4JFT
    CBAOT-2EKQ3-9WUOW-5L5XK-GCSR2
    OYFQY-NJXOD-NJV4C-DU36O-1MDIG
    RKS48-UQ19F-ODU4Q-725HO-9LOZN
    GQDY5-PIY4V-88K5F-HBBYG-ILHKL
    VB4OF-TX9T6-BBB4C-ZQBYQ-GDIQ1
    G53PN-W5WM7-7C6DI-8NFU8-1884H
    E68KB-1F4SE-DZL46-8K76N-WT9Y6
    226QR-P7HPJ-QXJU1-P859Z-V35KX
    ID7PP-LZCZQ-R856C-FY5R7-5RSZW
    VL2Q5-PECT5-FVZ89-KFTFL-NW77N
    B9GXD-YMDSM-2R3YI-VKPIJ-5H5FN
    I6UOB-P5Z8T-FXM1H-POWAE-5LC5X
    ENJBP-5BW9O-VJQO5-9DKF3-HYF6T
    B6SZH-RLS7H-E7JI5-7PQEE-NFK7O
    GXKXB-1RY9D-8WVWB-6FKUF-12AD3
    IBGK8-BW3AB-MUE44-DOMNA-K6M53
    S8LDV-I2G6W-1AW7J-65YCE-Z56K3
    O56GQ-PZX11-8SZAQ-TP4US-MLWTT
    9SZOE-72E4B-OBTV8-F7B6Q-BXB7F
    QAVO1-T5J5O-RCA5J-UGUD5-P2G3V
    8YXBJ-CH712-P7PO9-3X5KR-HDVND
    594NT-XBFI4-GV1GZ-BPUQ2-QOW1S
    SMLYB-TJ3IK-B4VU3-T3D2F-BFN5S
    ZC78I-DX45K-XC17F-FFDUU-ITOPI
    JZ79B-YX3DX-2B52F-XB6H6-FS3JO
    3B8QH-XIV73-5JP86-L6JPK-59Z75
    IYT3W-K4UYE-DSPRG-ZEPM5-WK5NG
    IXEI5-V5AXT-JPDLD-RLA11-GE5IY
    J8NBM-AVYD6-RF9T1-TAEZP-C2BEM
    95RW5-POJZ3-5GRQL-NTSE4-FTCGB
    B4ZWC-Q6SGE-J1Z7A-265K2-HS363
    LCJB1-XD62F-HF9G9-WC5SW-G1OGK
    U3Y7X-7WL4M-P968Y-GRH73-5KCRL
    UN819-TYGXI-N1RI1-ON1U3-I9C87
    GPSRS-B6PCO-GM47Y-7M5FP-KVWYF
    HQGCN-UUUAT-DWJZ2-R3AXM-EXE98
    L7SAR-C6SSV-TIV7J-ZJPZS-LW2VI
    N6ALV-3NMGX-76KGD-JAUNU-PCQPP
    UY3AY-XQW2Q-ZUD4U-D1L8Q-L7RA8
    2MH29-6UBW7-S42UX-FK7QO-AMURT
    9GMCH-82UUU-Q4UEM-UWQLP-ZZ5LV
    PAADV-86NSZ-Q269K-LZV9R-WF7Z8
    W5W6D-WXH75-WEVVL-MFGNZ-UJ7Z5
    Last edited by Zack Davidson; June 30th, 2014 at 04:26 PM.


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Have I done this efficiently ?

    Can you define "efficient"? Then maybe we could answer your question.

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Have I done this efficiently ?

    Quote Originally Posted by Cornix View Post
    Can you define "efficient"? Then maybe we could answer your question.
    Is there a better way to doing, or is this a fast and neat way of doing this? If you know what I mean

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,326
    Thanks
    53
    Thanked 2,393 Times in 2,347 Posts

    Default Re: Have I done this efficiently ?

    What is the purpose of this statement:
      code += "";
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jun 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Have I done this efficiently ?

    Quote Originally Posted by Norm View Post
    What is the purpose of this statement:
      code += "";
    Oh haha, yea just deleted that right there thanks man

  6. #6
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Have I done this efficiently ?

    I have some general tips for you:
    1) If you want to define constant values (like in C or C++) you dont use "static" but instead "static final" for variables.
    A static variable is not constant, its value can still be changed by the program at any point in time. Judging from your code I would assume that you want to make "repeat", "characters" and "dash" to static final variables. In this case it is also convention to write the variable names in all uppercase.

    2) This method:
    	private static int randomize(int range) {
    		return (int) (java.lang.Math.random() * (range + 1));
    	}
    is pointless because the Random class already gives you access to a method which does practically the same.
    Just create an object of the Random class instead of using the static Math methods, those are not recommended anyways.

    3) Why does your class have a static variable called "code"? This variable is not used at all.

    4) When modifying Strings you should use StringBuilder instead of concatenating using the "+"-operator. Read up on the StringBuilder documentation for more information on its uses and purpose.

  7. #7
    Junior Member
    Join Date
    Jun 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Have I done this efficiently ?

    Quote Originally Posted by Cornix View Post
    I have some general tips for you:
    1) If you want to define constant values (like in C or C++) you dont use "static" but instead "static final" for variables.
    A static variable is not constant, its value can still be changed by the program at any point in time. Judging from your code I would assume that you want to make "repeat", "characters" and "dash" to static final variables. In this case it is also convention to write the variable names in all uppercase.

    2) This method:
    	private static int randomize(int range) {
    		return (int) (java.lang.Math.random() * (range + 1));
    	}
    is pointless because the Random class already gives you access to a method which does practically the same.
    Just create an object of the Random class instead of using the static Math methods, those are not recommended anyways.

    3) Why does your class have a static variable called "code"? This variable is not used at all.

    4) When modifying Strings you should use StringBuilder instead of concatenating using the "+"-operator. Read up on the StringBuilder documentation for more information on its uses and purpose.
    Thanks a lot for all the tips, I'll change up the variable names, and also code is being used isn't it? By main and by random

  8. #8
    Member
    Join Date
    Jun 2014
    Posts
    33
    My Mood
    Inspired
    Thanks
    9
    Thanked 1 Time in 1 Post

    Default Re: Have I done this efficiently ?

    			"1", "2", "3", "4", "5", "5", "6", "7", "8", "9",
    			"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"
    Efficient or not, I would probably try to find some way to generate a couple tuples of character types; The first one would include the numerical characters and the second one would include the (upper case) letters. Then I would merge (copy) the content of these arrays into the final keys array (probably also containing character types and not strings).

    It wouldn't be easier to do, and it might not even take less code to achieve. But just as an exercise in elegance. :-) Imagine you were using a hundred or so characters in your codes - this is where the efficiency would code in. At least as far as coding is concerned.

    To take this even further - perhaps it would be possible to generate these codes without using an array of characters? Probably by using the same method theorized above for creating the numerical and alphabetical arrays respectively.

    I'm currently not sure how to do what I suggest myself, but I'm guessing that all characters are indexed somewhere and can be readily accessed by some method in the Java API. I'm sure someone else could point us in the right direction, but the fun is in figuring these things out for yourself, not? ;-)

  9. #9
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Have I done this efficiently ?

    Quote Originally Posted by Zack Davidson View Post
    Thanks a lot for all the tips, I'll change up the variable names, and also code is being used isn't it? By main and by random
    Nope.
    You have a method called "generateCode", this method is returning a String. The returned String is being used by your main method.
    Now, this String just happened to be the same String that is referenced by the variable code, but that does not mean that the variable is useful.
    If you remove the static variable code your program would still do the same.

  10. #10
    Junior Member
    Join Date
    Jun 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Have I done this efficiently ?

    Quote Originally Posted by Cornix View Post
    I have some general tips for you:
    1) If you want to define constant values (like in C or C++) you dont use "static" but instead "static final" for variables.
    A static variable is not constant, its value can still be changed by the program at any point in time. Judging from your code I would assume that you want to make "repeat", "characters" and "dash" to static final variables. In this case it is also convention to write the variable names in all uppercase.

    2) This method:
    	private static int randomize(int range) {
    		return (int) (java.lang.Math.random() * (range + 1));
    	}
    is pointless because the Random class already gives you access to a method which does practically the same.
    Just create an object of the Random class instead of using the static Math methods, those are not recommended anyways.

    3) Why does your class have a static variable called "code"? This variable is not used at all.

    4) When modifying Strings you should use StringBuilder instead of concatenating using the "+"-operator. Read up on the StringBuilder documentation for more information on its uses and purpose.
    how does it look now

    --- Update ---

    Quote Originally Posted by Cornix View Post
    Nope.
    You have a method called "generateCode", this method is returning a String. The returned String is being used by your main method.
    Now, this String just happened to be the same String that is referenced by the variable code, but that does not mean that the variable is useful.
    If you remove the static variable code your program would still do the same.
    yea ive changed this up a bit

  11. #11
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Have I done this efficiently ?

    Still the same, but now with a StringBuilder instead of the code variable.
    That StringBuilder does not need to be a static variable, just make it local within the generateCode method.

  12. #12
    Junior Member
    Join Date
    Jun 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Have I done this efficiently ?

    Quote Originally Posted by Cornix View Post
    Still the same, but now with a StringBuilder instead of the code variable.
    That StringBuilder does not need to be a static variable, just make it local within the generateCode method.
    Ok done

    --- Update ---

    Edit: i need it because of the main method

Similar Threads

  1. Help in recoding more efficiently
    By Bob M in forum What's Wrong With My Code?
    Replies: 15
    Last Post: December 25th, 2013, 01:49 PM
  2. [SOLVED] Downloading files efficiently
    By angstrem in forum Java Theory & Questions
    Replies: 8
    Last Post: June 9th, 2013, 05:07 AM
  3. how to data parse efficiently
    By coder13 in forum JDBC & Databases
    Replies: 0
    Last Post: April 4th, 2013, 06:12 PM
  4. Efficiently processing data ....
    By rdegrijs in forum Java Servlet
    Replies: 0
    Last Post: July 10th, 2012, 06:25 PM
  5. Using arrays more efficiently in Java?
    By mjballa in forum Java Theory & Questions
    Replies: 1
    Last Post: February 4th, 2012, 07:53 PM