Sunday, March 20, 2016

6-Digit Password Memorizer

This program converts a 6-digit numeric passcode into an English word (a=1, b=2, etc) so that it would be easier to memorize. Regular expression is used twice in this program, to check whether the input string contains digits (0-9) only and to extract the words from the content of the text file (words.txt).



  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.util.regex.*;
import javax.swing.*;

public class PasswordMemorizer extends JFrame{
    private JTextField tfpass=null;
    private JPanel panel=null;
    private JLabel lsuggest=null;
    private JButton bgenerate=null;
    
    public PasswordMemorizer(){
        super("Password Memorizer");
        setSize(300,120);
        
        tfpass=new JTextField();
        tfpass.setColumns(15);
        panel=new JPanel();
        lsuggest=new JLabel();
        bgenerate=new JButton("Generate");
        bgenerate.addActionListener(new ButtonHandler());
        
        setLayout(new BorderLayout());
        add("North",tfpass);
        panel.add(lsuggest);
        add("Center",panel);
        add("South",bgenerate);
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
    
    class ButtonHandler implements ActionListener{
        public void actionPerformed(ActionEvent e){
            String pwd=tfpass.getText();
            if(pwd.length()!=6){
                return;
            }
            Pattern pattern=Pattern.compile("\\d{6}");
            Matcher matcher=pattern.matcher(pwd);
            boolean found=false;
            while(matcher.find()){
                found=true;
            }
            if(!found){
                return;
            }
            
            try{
                Scanner sc=new Scanner(new FileInputStream("words.txt"));
                StringBuffer sbuf=new StringBuffer();
                sbuf.append(sc.nextLine());
                String regex=" ";
                Pattern p=Pattern.compile(regex);
                String[] items=p.split(sbuf);
                for(int i=0;i<items.length;i++){
                    String item="";
                    for(int j=0;j<items[i].length();j++){
                        String num=items[i].charAt(j)+"";
                        switch(num){
                            case "a":
                                num="1";
                                break;
                            case "b":
                                num="2";    
                                break;
                            case "c":
                                num="3";    
                                break;
                            case "d":
                                num="4";    
                                break;
                            case "e":
                                num="5";    
                                break;
                            case "f":
                                num="6";    
                                break;
                            case "g":
                                num="7";    
                                break;
                            case "h":
                                num="8";    
                                break;
                            case "i":
                                num="9";
                                break;
                            case "j":
                                num="10";    
                                break;
                            case "k":
                                num="11";
                                break;
                            case "l":
                                num="12";
                                break;
                            case "m":
                                num="13";    
                                break;
                            case "n":
                                num="14";    
                                break;
                            case "o":
                                num="15";    
                                break;
                            case "p":
                                num="16";
                                break;
                            case "q":
                                num="17";    
                                break;
                            case "r":
                                num="18";
                                break;
                            case "s":
                                num="19";
                                break;
                            case "t":
                                num="20";
                                break;
                            case "u":
                                num="21";
                                break;
                            case "v":
                                num="22";
                                break;
                            case "w":
                                num="23";    
                                break;
                            case "x":
                                num="24";
                                break;
                            case "y":
                                num="25";
                                break;
                            case "z":
                                num="26";
                                break;
                        }
                        item=item+num;
                    }
                    if(pwd.equals(item)){
                        lsuggest.setLocation(panel.getWidth()/2,panel.getHeight()/2);
                        lsuggest.setText(items[i]);
                    }
                    item="";
                }
            }catch(Exception ex){
                
            }
        }
    }
    
    public static void main(String[] args){
        new PasswordMemorizer();
    }
}

No comments:

Post a Comment