Page 2 of 2 FirstFirst 12
Results 21 to 26 of 26
Like Tree1Likes

Thread: How to create random passwords

  1. #21
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,916
    Rep Power
    5

    Default Re: How to create random passwords

    Please don't do line 6 and show all of your code. It makes it very difficult to assist.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  2. #22
    Snake006 is offline Senior Member
    Join Date
    Jun 2013
    Posts
    127
    Rep Power
    0

    Default Re: How to create random passwords

    ok, here we go:

    p
    Java Code:
    ublic class nearport() extends javax.swing.JFrame{
    
    
    public static final String[] passwords = {"Julia", "Jade", "Sopheria", "Venus"};
    private char[] char_array;
    private char[] correctPassword;
    
    public nearPort() {
            
            initComponents();  //graphics stuff, buttons etc
            this.setLocationRelativeTo(null);
        }
    
     private void PirateActionPerformed(java.awt.event.ActionEvent evt) {                                       
    
        JPanel userPanel = new JPanel();
        userPanel.setLayout(new GridLayout(2,2));
        JLabel passwordLbl = new JLabel("Password:");
        JPasswordField passwordFld = new JPasswordField();
        userPanel.add(passwordLbl);
        userPanel.add(passwordFld);
        int input = JOptionPane.showConfirmDialog(null, userPanel, "Enter your password:"
                          ,JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
        char[] enteredPassword = passwordFld.getPassword();
        Random ran=new Random();
     
         String s_ran = passwords[ran.nextInt(passwords.length)];
    
         
        correctPassword = char_array;
        System.out.print("Password is:"+s_ran);
    
    boolean isCorrect = Arrays.equals(correctPassword, enteredPassword);
    Arrays.fill(enteredPassword, '0');
            if(isCorrect){
                Dialog.showMessage("You gave the right password!");
            }
            else{
                Dialog.showMessage("You gave the wrong password");
            }
            
          
        }     
    }
    Last edited by Snake006; 08-01-2013 at 08:57 PM.

  3. #23
    Snake006 is offline Senior Member
    Join Date
    Jun 2013
    Posts
    127
    Rep Power
    0

    Default Re: How to create random passwords

    This code will randomly select a password from the list and will print it out however putting in the right password doesn't ever seem to display the dialog saying I gave the right password. Could it be that it randomly selects one password required for the password field and randomly selects another password and prints out it on the console?

  4. #24
    Snake006 is offline Senior Member
    Join Date
    Jun 2013
    Posts
    127
    Rep Power
    0

    Default Re: How to create random passwords

    Oh wait I figured it out. This part of the code needed to look like this instead:

    Java Code:
    String s_ran = passwords[ran.nextInt(passwords.length)];
    
         
        correctPassword = s_ran.toCharArray();
        System.out.print("Password is:"+s_ran);
    It works now

  5. #25
    Snake006 is offline Senior Member
    Join Date
    Jun 2013
    Posts
    127
    Rep Power
    0

    Default Re: How to create random passwords

    Ok last question. I was wondering how I might make it so that it will tell me what the password is before I actually enter it in by clicking on another button on my JFrame. This other button is actually another method and simply looks like this:

    Java Code:
    private void TavarnActionPerformed(java.awt.event.ActionEvent evt) {                                       
            Dialog.showMessage("You overhear a rough bunch talking.  The password of the day is"+Therandompassword);
            
        }
    At the moment of course the randompassword is nothing just a placeholder. It doesn't actually choose the random password until I click the pirate button. I want it so that the password given in the tavarn is the same password required to board the pirate ship. Any ideas?

  6. #26
    Snake006 is offline Senior Member
    Join Date
    Jun 2013
    Posts
    127
    Rep Power
    0

    Default Re: How to create random passwords

    Actually never mind again, I figured that one out, I just needed it to choose the random password in the constructor and that takes care of everything.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Hardcoded Passwords
    By k1ng in forum Advanced Java
    Replies: 10
    Last Post: 05-18-2012, 10:33 AM
  2. How do I create a code that generates random text?
    By Maya Berkowitz in forum New To Java
    Replies: 7
    Last Post: 02-08-2012, 12:01 PM
  3. HELP! not sure how to create random Array
    By vanilla88 in forum New To Java
    Replies: 3
    Last Post: 09-30-2011, 11:10 AM
  4. Hello? How to Create Random access???
    By Miyaki in forum New To Java
    Replies: 1
    Last Post: 03-05-2009, 07:37 AM
  5. Replies: 0
    Last Post: 12-15-2007, 09:29 AM

Posting Permissions

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