-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputParser.java
More file actions
66 lines (60 loc) · 1.78 KB
/
InputParser.java
File metadata and controls
66 lines (60 loc) · 1.78 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
56
57
58
59
60
61
62
63
64
65
66
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class InputParser {
public static void main(String[] args) {
ArrayList<String> inputs = new ArrayList<String>();
inputs.add("qwertyuytresdftyuioknn");
inputs.add("gijakjthoijerjidsdfnokg");
inputs.add("polkjuy");
for (String input : inputs) {
System.out.println(findWords(input));
}
}
public static ArrayList<String> findWords(String input) {
String firstLetter = input.substring(0, 1);
String lastLetter = input.substring(input.length() - 1);
ArrayList<String> choices = getChoices(firstLetter, lastLetter);
ArrayList<String> matches = new ArrayList<String>();
for (String choice : choices) {
String inputCopy = input;
String choiceCopy = choice;
while (inputCopy.length() > 0 && choiceCopy.length() > 0) {
if (inputCopy.charAt(0) == choiceCopy.charAt(0)) {
if (choiceCopy.length() >= 2 && choiceCopy.charAt(0) == choiceCopy.charAt(1)) {
choiceCopy = choiceCopy.substring(2);
} else {
choiceCopy = choiceCopy.substring(1);
}
}
inputCopy = inputCopy.substring(1);
}
if (choiceCopy.isEmpty()) {
matches.add(choice);
}
}
return matches;
}
public static ArrayList<String> getChoices(String firstLetter, String lastLetter) {
ArrayList<String> strings = new ArrayList<String>();
Scanner scanner = null;
try {
scanner = new Scanner(new File("enable1.txt"));
while (scanner.hasNextLine()) {
String word = scanner.nextLine();
if (word.length() >= 5 && word.substring(0, 1).equals(firstLetter)
&& word.substring(word.length() - 1).equals(lastLetter)) {
strings.add(word);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (scanner != null) {
scanner.close();
}
}
return strings;
}
}