dsta
dsta

Reputation: 373

Java Random Password Generator

I made this to help me practice User Interface. For some reason the password doesn't display on the screen when Generate! is pressed. There are no program errors either. As you can see I have a JLabel for the password.

Code:

package components; 
import java.io.*;
import java.util.Scanner;
import java.util.Random;
import javax.swing.*;
import javax.*;
import java.awt.*;
import java.awt.event.*;

public class PassGenButton extends JPanel implements ActionListener{

protected JButton generate;
protected JLabel passLabel;
public String password = null;

public PassGenButton()
{
    JButton generate = new JButton("Generate!");
    JLabel passLabel = new JLabel(password, JLabel.CENTER);
    passLabel.setFont(new Font("Serif", Font.PLAIN, 36));
    passLabel.setBorder(BorderFactory.createTitledBorder("Password"));
    setLayout(new BorderLayout());
    generate.addActionListener(this);
    add(generate, BorderLayout.SOUTH);
    add(passLabel, BorderLayout.CENTER);
}

public void actionPerformed(ActionEvent e)
{
    GetPassword();
}

private static void createAndShowGUI()
{
    JFrame frame = new JFrame("Password Generator");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    PassGenButton contentPane = new PassGenButton();

    frame.setContentPane(contentPane);
    frame.setSize(400, 200);
    frame.setLocation(600, 300);
    frame.setVisible(true);
}   

public static void main(String[] args)
{
    javax.swing.SwingUtilities.invokeLater(new Runnable(){
        public void run(){
            createAndShowGUI();     
        }
    });
}

public void GetPassword()
{
    password = null;
    String[] nouns = new String[2432];
    File file = new File("C:\\Temp\\nounlist.txt");
    String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    char[] characters = chars.toCharArray();

    try
    {
        nouns = ReadTextFile(file);
    }
    catch (FileNotFoundException f)
    {
        f.getMessage();
        System.exit(1);
    }

    ShowPassword(nouns, characters);
}

public final String[] ReadTextFile(File aFile) throws FileNotFoundException
{
    String[] strings = new String[2432];
    int counter = 0;
    Scanner scanner = new Scanner(new FileReader(aFile));

    try
    {
        while (scanner.hasNextLine())
        {
            strings[counter] = scanner.nextLine();
            counter++;
        }
    }

    finally
    {
        scanner.close();
    }

    return strings;
}

public void ShowPassword(String[] nouns, char[] characters)
{
    String password;
    Random generator = new Random();
    int chosenNoun = 0;
    int chosenChar = 0;
    int int1 = 0;
    int int2 = 0;

    chosenNoun = generator.nextInt(2432);
    chosenChar = generator.nextInt(26);
    int1 = generator.nextInt(10);
    int2 = generator.nextInt(10);

    password = nouns[chosenNoun] + characters[chosenChar] + Integer.toString(int1) + Integer.toString(int2);
}
}

Upvotes: 1

Views: 3481

Answers (4)

Koray Güclü
Koray Güclü

Reputation: 2898

you can use ostermillers password generator. He also have examples on his website and a javaapplet http://ostermiller.org/utils/src/RandPass.java.html

Upvotes: 0

Amos M. Carpenter
Amos M. Carpenter

Reputation: 4958

You're hiding your passLabel variable (as well as your generate button). You've already declared them as instance variables, you don't need to re-declare them in your constructor, just assign values to them. So instead of:

JButton generate = new JButton("Generate!");
JLabel passLabel = new JLabel(password, JLabel.CENTER);

you need to have:

generate = new JButton("Generate!");
passLabel = new JLabel(password, JLabel.CENTER);

Then you need to make sure you set its text as per bdares' answer.

Upvotes: 2

user684934
user684934

Reputation:

Your passLabel label just sits there. Its value never changes. Your showPassword() method presumably is supposed to display the given password, but it doesn't. It simply creates a string with the password's value, then ends, without ever even looking at the label.

You need a last line reading something like this:

passLabel.setText(password);

Upvotes: 2

Andrew Thompson
Andrew Thompson

Reputation: 168825

Edited snippet

public void GetPassword()
{
    password = null;
    String[] nouns = new String[2432];
    File file = new File("C:\\Temp\\nounlist.txt");
    String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    char[] characters = chars.toCharArray();

    try
    {
        nouns = ReadTextFile(file);
    }
    catch (FileNotFoundException f)
    {
        f.printStackTrace();
        //System.exit(1);
    }

    ShowPassword(nouns, characters);
}

Output

java.io.FileNotFoundException: C:\Temp\nounlist.txt (The system cannot find the path specified)
       at java.io.FileInputStream.open(Native Method)
       at java.io.FileInputStream.<init>(FileInputStream.java:120)
       at java.io.FileReader.<init>(FileReader.java:55)
       at PassGenButton.ReadTextFile(PassGenButton.java:78)
       at PassGenButton.GetPassword(PassGenButton.java:63)
       at PassGenButton.actionPerformed(PassGenButton.java:29)
       ...

The output might not be exactly the same where you are, but change the method as specified and copy/paste the output.

Upvotes: 2

Related Questions