To get you started on Project 6, here is the basic code to read the checkme.txt
file. It reads words, converts to upper case, and strips extraneous punctuation.
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
public class Main {
public static String removeNonLetDig(String s) {
int b = 0, e = s.length()-1;
// Trim from the beginning
while (b <= e && !Character.isLetterOrDigit(s.charAt(b)))
b++;
// Trim from the end
while (e >= b && !Character.isLetterOrDigit(s.charAt(e)))
e--;
if (b <= e)
return s.substring(b,e + 1);
else
return null;
}
public static void main(String[] args) throws FileNotFoundException {
Scanner inFile = new Scanner(new FileReader("checkme.txt"));
while (inFile.hasNext()) {
String word = inFile.next().toUpperCase();
String stripped = removeNonLetDig(word);
System.out.printf("Original %-15s Modified %-15s%n", word, stripped);
}
inFile.close();
}
}
The output shows that null
is possible. Remember that this indicates that no letters (or digits) remained after processing. This is not a misspelled word; it's not a word at all.
Original -*- Modified null Original OUTLINE Modified OUTLINE Original -*- Modified null Original THIS Modified THIS Original DIRECTORY Modified DIRECTORY Original CONTAINS Modified CONTAINS Original BISON Modified BISON Original SKELETONS: Modified SKELETONS ...