JAVA's first Sample, example ample
I recently learned java and completed the entry in two and a half days (from January 1, April 21-20, 2015 to August 1, April 23.
Write a small Sample to generate a random password for learning. In April 22, the console version was written. On the morning of April 9, April 23, a GUI version was written. This is a java entry. Android Application was written in April 24.
Paste the source code of the console version. I hope you can correct it.
Console version running effect:
Gui version running effect:
Console source code
RandomPasswordSample2.java
1 // by JIURL
2 // Weibo http://weibo.com/ddqqppb
3 //
4
5 import java. util. collections;
6
7 public class RandomPasswordSample2 {
8
9 public static void main (String [] args ){
10 // TODO Auto-generated method stub
11 System. out. printf ("******************************* \ n ");
12 System. out. printf ("Random Password Generator \ n ");
13 System. out. printf ("by JIURL \ n ");
14 System. out. printf ("******************************* \ n ");
15
16 int nPasswordLength;
17 System. out. printf ("input password length :");
18 Pipeline SC = new pipeline (System. in );
19 nPasswordLength = SC. nextInt ();
20 SC. close ();
21
22 // generate nPass passwords one time to choose
23 final int nPass = 10;
24 RandomPassword [] rndpass = new RandomPassword [nPass];
25
26 int I;
27 for (I = 0; I <nPass; I ++)
28 {
29 rndpass [I] = new RandomPassword (nPasswordLength );
30}
31
32 // display the results
33 for (I = 0; I <nPass; I ++)
34 {
35 System. out. printf ("% s \ n", rndpass [I]. GetRandomPassword ());
36}
37}
38}
RandomPassword. java 1 // by JIURL
2 // Weibo http://weibo.com/ddqqppb
3 //
4
5 import java. util. Random;
6
7 public class RandomPassword {
8 private String strRandomPassword;
9 private int nRandomPasswordLength;
10
11 private final static char caAlphabet [] =
12 {
13 'A', 'B', 'C', 'D', 'E', 'E', 'F', 'G', 'h', 'I ', 'J', 'k', 'l', 'M ',
14 'n', 'O', 'P', 'Q', 'R', 's', 't', 'U', 'V ', 'w', 'x', 'y', 'z ',
15 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
16 };
17
18 public RandomPassword (int nRandomPasswordLength ){
19 this. nRandomPasswordLength = nRandomPasswordLength;
20 this. strRandomPassword = CreateRandomPassword (nRandomPasswordLength );
21}
22
23 private String CreateRandomPassword (int nRandomPasswordLength ){
24 int I;
25
26 Random rnd = new Random ();
27 rnd. setSeed (System. nanoTime ());
28
29 StringBuilder strBuilder = new StringBuilder ("");
30
31 int x;
32 x = Math. abs (rnd. nextInt () % 26;
33
34 strBuilder. append (RandomPassword. caAlphabet [x]);
35 for (I = 1; I <nRandomPasswordLength; I ++)
36 {
37 x = Math. abs (rnd. nextInt () % 36;
38 strBuilder. append (RandomPassword. caAlphabet [x]);
39}
40
41 strRandomPassword = strBuilder. toString ();
42
43 return this. strRandomPassword;
44}
45
46 public String GetRandomPassword (){
47 return strRandomPassword;
48}
49}
Weibo: http://weibo.com/ddqqppb