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

Thread: Help with password generation

  1. #1
    Member
    Join Date
    Jun 2012
    Location
    Uppsala, Sweden
    Posts
    36
    Thanks
    10
    Thanked 1 Time in 1 Post

    Post Help with password generation

    I'm making a program to generate a random password with the desired length and chars.

    All went smooth until i got to the generation itself. The error exists in this code:

    package generate;
     
    import gui.PassPanel;
     
    import java.util.Random;
     
    public class GeneratePass {
     
    	private final String[] letters = new String[] {
    			"a","A","b","B","c","C","d","D","e","E","f","F","g","G","h","H",
    			"i","I","j","J","k","K","l","L","m","M","n","N","o","O","p","P",
    			"q","Q","r","R","s","S","t","T","u","U","v","V","w","W","x","X",
    			"y","Y","z","Z"
    	};
     
    	private final String[] numbers = new String[] {
    			"0","1","2","3","4","5","6","7","8","9"
    	};
     
    	private final String[] both = new String[] {
    			"a","A","b","B","c","C","d","D","e","E","f","F","g","G","h","H",
    			"i","I","j","J","k","K","l","L","m","M","n","N","o","O","p","P",
    			"q","Q","r","R","s","S","t","T","u","U","v","V","w","W","x","X",
    			"y","Y","z","Z","0","1","2","3","4","5","6","7","8","9"
    	};
     
    	String pass = "";
     
    	public GeneratePass(String chars, int length) {
    		String[] charsUsing;
    		if (chars.equals("both")) {
    			charsUsing = both;
    		}
    		else if (chars.equals("letters")) {
    			charsUsing = letters;
    		}
    		else{
    			charsUsing = numbers;
    		}
     
    		Random r = new Random(charsUsing.length);
     
    		for (int i = 0; i < length; i++) {
    			pass = pass + charsUsing[r.nextInt()];
    		}
     
    		PassPanel.setPassword(pass);
    	}
    }

    And I get this exception:

     
    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1165488049
    at generate.GeneratePass.<init>(GeneratePass.java:44)
    at gui.PassPanel$MyOkButtonListener.actionPerformed(P assPanel.java:103)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2012)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2335)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:404)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.jav a:6268)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:603 3)
    at java.awt.Container.processEvent(Container.java:204 5)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4629)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2103)
    at java.awt.Component.dispatchEvent(Component.java:44 55)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4633)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4297)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4227)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2089)
    at java.awt.Window.dispatchEventImpl(Window.java:2517 )
    at java.awt.Component.dispatchEvent(Component.java:44 55)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:649)
    at java.awt.EventQueue.access$000(EventQueue.java:96)
    at java.awt.EventQueue$1.run(EventQueue.java:608)
    at java.awt.EventQueue$1.run(EventQueue.java:606)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:105)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:116)
    at java.awt.EventQueue$2.run(EventQueue.java:622)
    at java.awt.EventQueue$2.run(EventQueue.java:620)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:105)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 619)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:275)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:200)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:190)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:185)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:177)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:138)




    It points to this line of code:


    pass = pass + charsUsing[r.nextInt()];

    Please help me!
    Last edited by Dr.Code; June 26th, 2012 at 10:54 AM.


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Help with password generation

    You just need to re-read the API documentation for Random. The constructor you're using doesn't do what you think it does. Nor does Random.nextInt(). The fix is to select the correct constructor and the correct int-returning method from Random's API

  3. The Following User Says Thank You to Sean4u For This Useful Post:

    Dr.Code (June 26th, 2012)

  4. #3
    Member
    Join Date
    Jun 2012
    Location
    Uppsala, Sweden
    Posts
    36
    Thanks
    10
    Thanked 1 Time in 1 Post

    Default Re: Help with password generation

    Quote Originally Posted by Sean4u View Post
    You just need to re-read the API documentation for Random. The constructor you're using doesn't do what you think it does. Nor does Random.nextInt(). The fix is to select the correct constructor and the correct int-returning method from Random's API
    Thank you! I see what I did now and it works perfect, thanks!

Similar Threads

  1. PDF generation
    By deependeroracle in forum JDBC & Databases
    Replies: 2
    Last Post: June 26th, 2012, 10:29 AM
  2. Jpa Primary key generation
    By rdheepan in forum JDBC & Databases
    Replies: 5
    Last Post: December 13th, 2011, 03:44 AM
  3. Jaxb-- java file generation for DTD
    By tcstcs in forum Java Theory & Questions
    Replies: 0
    Last Post: November 22nd, 2011, 02:21 AM
  4. Okay Here Goes...World and Map Generation.
    By rooster5105 in forum Algorithms & Recursion
    Replies: 1
    Last Post: October 30th, 2011, 02:45 PM
  5. Is AgileUnit the next test generation?
    By ricardo7307 in forum Member Introductions
    Replies: 0
    Last Post: May 16th, 2011, 02:33 PM

Tags for this Thread