Repository navigation
Expand file tree
/
Copy pathCopyReadingFiles.java
More file actions
115 lines (102 loc) · 3.46 KB
/
Copy pathCopyReadingFiles.java
File metadata and controls
115 lines (102 loc) · 3.46 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
115
package Hangman;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class CopyReadingFiles {
public static void main(String[] args) throws FileNotFoundException {
String fileName = "words.txt";
ArrayList<String> words = createArrayList(fileName);
String fileName2 = "out.txt";
ArrayList<String> badWords = createArrayList(fileName2);
// looking for sunshine
System.out.println("Index of Sunshine: " + words.indexOf("sunshine"));
int theSize = words.size();
System.out.println("The array list size: " + theSize);
//array list changed into array
String[] wordList = convertToArray(words);
//look for sunshine
for (int item = 0; item < theSize; item++) {
if (wordList[item].equalsIgnoreCase("sunshine")) {
System.out.println("We found sunshine at: " + item);
}
}
// randomize the array of words
randomizeIT(wordList);
//look for sunshine
for (int item = 0; item < theSize; item++) {
if (wordList[item].equalsIgnoreCase("sunshine")) {
System.out.println("We found sunshine at: " + item);
}
}
} //---------------end of Main
/*
* compares the dictionary to the list of bad words and modifies the dictionary
*/
public static void barOfSoap(ArrayList<String> words, ArrayList<String> badWords){
for (int bad = 0; bad < badWords.size(); bad++) {
for (int ok = 0; ok < words.size(); ok++) {
if (badWords.get(bad).equals(words.get(ok)));
words.remove(ok);
}
}
}
/*
* create a method that returns an ArrayList with words of 8 letters
*/
public static ArrayList<String> eightLetterWords(ArrayList<String> words){
ArrayList<String> eights = new ArrayList<String>();
for (String item : words) {
if (item.length() < 9)
eights.add(item);
}
return eights;
}
/*
* this method allows you to mix up your alpha list, think about going
* across the array and swapping each element with another randomly picked element.
* You can use MakeARandom to find that new spot to do the swap.
* get a number for the index of a word and assign it to a new index
*/
private static void randomizeIT(String[] arr) {
for (int i = 0; i < arr.length; i++) {
int temp = i;
int swapIndex = makeARandom(0, arr.length-1);
arr[i] = arr[swapIndex];
arr[swapIndex] = arr[temp];
}
}
private static String[] convertToArray(ArrayList<String> words) {
String[] wordArray = new String[words.size()];
for (int i = 0; i < wordArray.length; i++) {
wordArray[i] = words.get(i);
}
return wordArray;
}
/*
* using a text file full of words, one word on each line, read in a line and put that line
* into the ArrayList. This method works, and you shouldn't need to do anything but understand it
*/
private static ArrayList<String> createArrayList(String theFileName) throws FileNotFoundException{
// read in the file
Scanner input = new Scanner(new File(theFileName));
// create an array list, to put the words into
ArrayList<String> words = new ArrayList<String>();
// read in the file, one line at a time
while (input.hasNextLine()) {
String line = input.nextLine();
// make everything lower case
line = line.toLowerCase();
if (line.length() < 15) {
words.add(line);
}
}
// Display the size of the array list
System.out.println("Size: " + words.size());
return words;
}
// picking a random number
private static int makeARandom(int low, int high) {
return (int) (Math.random() * high + low);
}
}