forked from allicen/Java-1000
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDictionary.java
More file actions
46 lines (39 loc) · 1.37 KB
/
Copy pathDictionary.java
File metadata and controls
46 lines (39 loc) · 1.37 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
package dictionary;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class Dictionary {
public static void main(String[] args) throws IOException {
ArrayList<String> words = new ArrayList<>();
String dictionary;
int wordsCount = 0;
Scanner sc = new Scanner(new FileReader("input.txt"));
while (sc.hasNextLine()) {
words.add(sc.nextLine());
}
dictionary = words.get(words.size()-1);
words.remove(words.size()-1);
words.remove(0);
for (String word : words) {
char[] dictionaryCopy = dictionary.toCharArray();
char[] characters = word.toCharArray();
boolean wordInDictionary = true;
for (char character : characters) {
if (!String.valueOf(dictionaryCopy).contains(String.valueOf(character))) {
wordInDictionary = false;
break;
} else {
dictionaryCopy[String.valueOf(dictionaryCopy).indexOf(String.valueOf(character))] = ' ';
}
}
if (wordInDictionary) {
wordsCount++;
}
}
PrintWriter out = new PrintWriter(System.out);
out.println(wordsCount);
out.flush();
}
}