-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScrambleWord.java
More file actions
55 lines (40 loc) · 1.18 KB
/
ScrambleWord.java
File metadata and controls
55 lines (40 loc) · 1.18 KB
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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class ScrambleWord {
public static void main(String[] args) throws Exception {
int number = 0;
String word = "", result = "";
File file = new File("wordlist.txt");
Scanner sc = new Scanner(file);
ArrayList<String> list = new ArrayList<String>();
while(sc.hasNext()) {
list.add(sc.nextLine());
}
for(int i = 0; i < 10; i++) {
number = (int) (list.size() * Math.random());
word = list.get(number);
result = shuffle(word);
System.out.println(result);
}
}
private static String shuffle(String word) {
String[] letters = new String[word.length()];
String temp = "", result = "";
int number = 0;
for(int i = 0; i < letters.length; i++) {
letters[i] = "" + word.charAt(i);
}
for(int i = 0; i < letters.length; i++) {
number = (int) (letters.length * Math.random());
temp = letters[number];
letters[number] = letters[i];
letters[i] = temp;
}
for(int j = 0; j < letters.length; j++) {
result = result + letters[j];
}
return result;
}
}