|
URL Example with Desktop class |
The Collections class which can be found within the java.util namespace provides two methods that suffle the elements of a Collection.
static void shuffle(List<?> list)
static void shuffle(List<?> list, Random
rnd)
The first method shuffles the elements according to a default source of randomness, while the second uses a specified source of randomness. The random number methods generate numbers with replacement. This means
that, a particular random number may be generated repeatedly.
The example below shows how to produce the values from 0 to 50 in a random order.
ShuffleExample.java
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class ShuffleExample {
public static void main(String args[]) {
String str[] = { “A”, “B”, “C”, “D”, “E” };
// Create a list1 with elements
List list1 = Arrays.asList(str);
Random rand = new Random(50);
// Shuffle the elements in the list
Collections.shuffle(list1, rand);
System.out.println(list1);
Collections.shuffle(list1, rand);
System.out.println(list1);
}
}
In this program, the output will be generated different each times when we shuffle the list of a Collection.
C:\Tips&Tricks>javac ShuffleTest.java
C:\Tips&Tricks>java ShuffleTest
[A, B, D, E, C]
[A, D, E, C, B]
Download Example
4. Password Prompting with |
|
java.io.Console
The new released JDK6 includes a new Console class, which can be found in the java.io package. This class adds some new features to enhance and simplify command-line applications. It includes a method specifically for reading passwords that disables console echo and
returns a char array for security purpose.
The example given below read the password from the console but the password will not be echoed to the console screen. If the given password matches with the specified characters as “javajazzup” then a message “Access granted” is displayed; otherwise it displays “Access denied” as an output.
PasswordPrompting.java
import java.io.Console;
import java.util.Arrays;
public class PasswordPrompting {
public static void main(String[] args) {
Console console = System.console();
if (console == null) {
System.out.println(“Console is not
available”);
System.exit(1);
}char[] password =
“javajazzup”.toCharArray();
/* Read password, the password will not be
echoed to the console screen and returned as
an array of characters.*/
char[] passwordEntered =
console.readPassword(“Enter password: “);
if (Arrays.equals(password,
passwordEntered)) {
System.out.println(“\n Access granted
\n”);
// Clear the password after validation
successful
Arrays.fill(password, ‘ ‘);
Arrays.fill(passwordEntered, ‘ ‘);
} else {
System.out.println(“Access denied”);
System.exit(1);
}
}
} |
|
|
March 2008 | Java Jazz Up | 54 |
|
|
View All Topics |
All Pages of this Issue |
Pages:
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,
Download PDF |
|
|
|
Mar
2008 | Java Jazz Up | 54 |
|
|
View All Topics |
All Pages of this Issue |
Pages:
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,
Download PDF |
|
|
|
|
|
|
|
|
|