Question

application will ask the user for an ID and password and whether they are an existing or new user. If you are a new user you will prompted to re-enter your details to confirm id and password and they will then be stored in the array. Once the user clicks the login button the application will search through an array of IDs and password for verification. (Set your array up to contain a maximum of 10 users – given it is a small array can use linear search) The application then will display an appropriate message.

my code so far

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class Log extends JFrame
{
    public static void main(String[] args)
    {
        Log frameTabel = new Log();
    }


        JLabel title = new JLabel("Please type your ID and Password");
        JLabel id = new JLabel("ID:");
        JLabel psword = new JLabel("Password:");
        JButton blogin = new JButton("Login");
        JPanel panel = new JPanel();
        JTextField txuser = new JTextField(15);
        JPasswordField pass = new JPasswordField(15);

        JRadioButton radNew = new JRadioButton("New", true);
        JRadioButton radExisting = new JRadioButton("Existing", false);

        ButtonGroup radioGroup1 = new ButtonGroup();

        Log()
        {
            super("Week 9 Question 3");
            setSize(300,250);
            setLocation(600,250);
            panel.setLayout (null);

            title.setBounds(40,5,300,20);
            id.setBounds(52,40,150,20);
            txuser.setBounds(70,40,150,20);
            psword.setBounds(7,80,150,20);
            pass.setBounds(70,80,150,20);
            blogin.setBounds(180,130,80,20);

            radNew.setBounds(30, 130, 50, 20);
            radExisting.setBounds(90, 130, 90, 20);

            panel.add(title);
            panel.add(id);
            panel.add(psword);
            panel.add(blogin);
            panel.add(txuser);
            panel.add(pass);
            add(radNew);
            add(radExisting);
            radioGroup1.add(radNew);
            radioGroup1.add(radExisting);

            map.put("test", "12345");


            getContentPane().add(panel);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);
            actionlogin();
        }

        public void actionlogin() {
            blogin.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    String puname = txuser.getText();
                    String ppaswd = new String(pass.getPassword());

                        JOptionPane.showMessageDialog(null, "Correct Information", "Correct",
                                JOptionPane.INFORMATION_MESSAGE);
                    } else {
                        JOptionPane.showMessageDialog(null, "No Password/ID Found on System",
                                "Incorrect", JOptionPane.INFORMATION_MESSAGE);
                        txuser.setText("");
                        pass.setText("");
                        txuser.requestFocus();

                        if (radNew.isSelected()) {

                            }

                            JOptionPane.showMessageDialog(null,
                                    "Re-enter your password and ID to confirm", "Information Saved",
                                    JOptionPane.INFORMATION_MESSAGE);
                        }
                    }
}

            });
        }
}
Was it helpful?

Solution

Use

 char[] ppaswd = pass.getPassword();

instead of

String ppaswd = pass.getText();

because

The method getText() from the type JPasswordField is deprecated.

As @MadProgrammer says:

The password is stored in a char array and you never convert it back to a String.

Strings live in the JVM for the life of JVM, making easier for nasty people examine the String pool and find the passwords.


Use a Map to store username/password.

 Map<String, char[]> map = new HashMap<String, char[]>();

put some initial values

    map.put("test", "12345".toCharArray());
    map.put("admin", "admin".toCharArray());

Here is the modified code

public void actionlogin() {
    blogin.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            String puname = txuser.getText();
            char[] ppaswd = pass.getPassword();
            if (map.keySet().contains(puname) && Arrays.equals(map.get(puname), ppaswd)) {
                JOptionPane.showMessageDialog(null, "Correct Information", "Correct",
                        JOptionPane.INFORMATION_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(null, "No Password/ID Found on System",
                        "Incorrect", JOptionPane.INFORMATION_MESSAGE);
                txuser.setText("");
                pass.setText("");
                txuser.requestFocus();

                if (radNew.isSelected()) {
                    if (!puname.equals("") && ppaswd.length > 0) {
                        map.put(puname, ppaswd);
                    }

                    JOptionPane.showMessageDialog(null,
                            "Re-enter your password and ID to confirm", "Information Saved",
                            JOptionPane.INFORMATION_MESSAGE);
                }
            }
        }
    });
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top