On a website I found some alternatives to "The quick brown fox jumps over the lazy dog" and I decided to write a little program to check if the alternatives were valid.
For those interested, I wrote the following program (using the filereader idea of this post) that checks a file with the sentences under each other:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class TestClass {
public static void main(String... aArgs) {
TestClass tc = new TestClass();
try {
String[] pieces = tc.splitFile("/home/user2609980/Desktop/text");
for (String line : pieces) {
if (line.contains("a") &&
line.contains("b") &&
line.contains("c") &&
line.contains("d") &&
line.contains("e") &&
line.contains("f") &&
line.contains("g") &&
line.contains("h") &&
line.contains("i") &&
line.contains("j") &&
line.contains("k") &&
line.contains("l") &&
line.contains("m") &&
line.contains("n") &&
line.contains("o") &&
line.contains("p") &&
line.contains("q") &&
line.contains("r") &&
line.contains("s") &&
line.contains("t") &&
line.contains("u") &&
line.contains("v") &&
line.contains("w") &&
line.contains("x") &&
line.contains("y") &&
line.contains("z")) {
System.out.println("Matches: " + line);
} else {
System.out.println("Does not match: " + line);
}
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
public String[] splitFile(String file) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(file));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append('\n');
line = br.readLine();
}
String everything = sb.toString();
String[] pieces = everything.split("\n");
return pieces;
} finally {
br.close();
}
}
}
And this was the output:
Matches: The quick brown fox jumps over the lazy dog
Does not match: Pack my box with five dozen liquor jugs.
Matches: Several fabulous dixieland jazz groups played with quick tempo.
Does not match: Back in my quaint garden, jaunty zinnias vie with flaunting phlox.
Does not match: Five or six big jet planes zoomed quickly by the new tower.
Matches: Exploring the zoo, we saw every kangaroo jump and quite a few carried babies.
Matches: I quickly explained that many big jobs involve few hazards.
Does not match: Jay Wolf is quite an expert on the bass violin, guitar, dulcimer, ukulele and zither.
Matches: Expect skilled signwriters to use many jazzy, quaint old alphabets effectively.
Matches: The wizard quickly jinxed the gnomes before they vaporized.
I want to improve this program in two ways. One, and that is my question, is how to make a more efficient piece of code instead of checking for each letter of the alphabet seperately. How can I make something like:
line.Contains([regex])
if that is possible?
The bonus question is how I can make this program so that it prints out exactly where it does not match. Of course I can do an if-else for every letter, but I hope there is a prettier way.
Thanks for your attention and I look forward to read your possible response.