-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpellBound.java
More file actions
114 lines (65 loc) · 2.13 KB
/
SpellBound.java
File metadata and controls
114 lines (65 loc) · 2.13 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import java.util.*;
import java.io.File;
import java.io.FileNotFoundException;
public class SpellBound{
public static void main(String[] args){
ArrayList<String> vocabFileLines = readVocabFile("beginners vocab list.txt");
ArrayList<Word> wordQueue = parseLines(vocabFileLines,";");
for (Word element : wordQueue){
System.out.println("Definition: " + element.showDefinition());
Scanner userInput = new Scanner(System.in);
String userAttempt = userInput.nextLine();
if (userAttempt.equals(element.showWord())){
System.out.println("Correct!");
}
else{
System.out.println("Wrong.");
}
}
}
//A method that reads a txt file and puts each line in an Array List.
//Input: filename
//Output: Array List where each entry is one line of the text file
public static ArrayList<String> readVocabFile(String filename){
ArrayList<String> arrayOfLines = new ArrayList<>();
int numberOfLines = 0;
try{
File vocabListFileName = new File(filename);
Scanner vocabList = new Scanner(vocabListFileName);
while (vocabList.hasNext()){
arrayOfLines.add(vocabList.nextLine());
numberOfLines++;
}
}
catch(FileNotFoundException ex){
System.out.println("File not found");
System.exit(1);
}
return arrayOfLines;
}
public static ArrayList<Word> parseLines(ArrayList<String> arrayOfLines, String delimiter){
ArrayList<Word> wordQueue = new ArrayList<>();
for (String line : arrayOfLines){
String[] splitArray = line.split(delimiter,2);
Word word1 = new Word(splitArray[0], splitArray[1]);
wordQueue.add(word1);
}
return wordQueue;
}
}
//Create Word class
class Word{
String word;
String definition;
Word(String vocabWord, String vocabWordDefinition){
int length = vocabWord.length();
private word = vocabWord;
private definition = vocabWordDefinition;
}
String showWord(){
return word;
}
String showDefinition(){
return definition;
}
}