Monday, 9 September 2013

Java Reading Words from a file with apostrophies and hypens and counting them as 2 words

Java Reading Words from a file with apostrophies and hypens and counting
them as 2 words

I am writing a program that will read two files in from the command line
and find how many words in each .txt file. I have got my code to work,
however I need it to count each apostrophe as 2 words and each hyphen as
two words. Something like this e.g., John's will become two separate words
"john" and "s" and Tick-Tock will become "tick" and "tock"). I cannot use
util.regex also. Here is my code"
private void readFile(){
Scanner scanner = null;
try {
scanner = new Scanner(file);
while (scanner.hasNextLine()){
String line = scanner.nextLine();
String[] wordOfLine = line.split("\\W");
lineNum++;//accumulate line
//counts all words
for (String word : wordOfLine){
if (word.equals("") || !(isWord(word)))
continue;
words.add(word.toLowerCase());
if (wordNum + 1 > wordNum){//in case of overflow
wordNum++;//accumulate word
}
else
throw new Error(" word number overflow!");
}
}
} catch (FileNotFoundException e) {
throw new Error("cannot find file!");
} finally{
if (scanner != null)
scanner.close();
}
System.out.println("Test for wordNum "+wordNum);
}
//check if text is word
private boolean isWord(String text) {
return text.matches("[a-zA-Z]+");
}

No comments:

Post a Comment