Counting Vowels and Prepositions in a text file
Hi,
I wanted help implementing how to count vowels and preposition in a text file..this is what i have for counting words, the other are blank because i simply can't get it to work..Help is greatly welcomed..
Code :
import java.io.*;
public class FileIO {
public static void main(String[] args) throws IOException {
countWords();
countVowels();
countPrepositions();
}
public static void countWords() throws IOException {
FileReader fr = new FileReader(
"G:\\Java 1302\\project\\FILEIO\\src\\input.txt");
BufferedReader br = new BufferedReader(fr);
StreamTokenizer stz = new StreamTokenizer(br);
int index = 0;
int numWords = 0;
while (index != StreamTokenizer.TT_EOF) {
index = stz.nextToken();
numWords++;
}
System.out.println("number of words in file = " + numWords);
}
public static void countVowels() throws IOException {
}
public static void countPrepositions() throws IOException {
System.out.println("number of prepositions in the file =");
}
}
Re: Counting Vowels and Prepositions in a text file
Please show me the text file you will be using..
I probably wouldn't use the StreamTokenizer.
I would read in the file like this: http://www.javaprogrammingforums.com...ner-class.html
And put each word into an array... You can then loop through the array to find you answers :)
Let me know if you need help and I will write an example..
Re: Counting Vowels and Prepositions in a text file
Quote:
Originally Posted by
JavaPF
Please show me the text file you will be using..
I probably wouldn't use the StreamTokenizer.
I would read in the file like this:
http://www.javaprogrammingforums.com...ner-class.html
And put each word into an array... You can then loop through the array to find you answers :)
Let me know if you need help and I will write an example..
hey i go it to work, but the problem is printing the outputs to the source file and counting preposition...
do u know how i could go about counting prepositions..this is my code for counting vowels, however, it's not writing to my output file...help
Code :
public class FileIO {
private static FileReader inputStream = null;
private static FileWriter outputStream = null;
public static void main(String[] args) throws Exception {
countWords();
}
public static void countVowels() throws IOException {
try {
inputStream = new FileReader(
" input.txt");
outputStream = new FileWriter(
"output.txt");
BufferedReader br = new BufferedReader(inputStream);
int numOfVowels = 0;
String lineRead;
while ((lineRead = br.readLine()) != null) {
for (int i = 0; i < lineRead.length(); i++) {
char c = lineRead.charAt(i);
if (c == 'a' || c == 'A' || c == 'e' || c == 'E'
|| c == 'i' || c == 'I' || c == 'o' || c == 'O'
|| c == 'u' || c == 'U') {
numOfVowels++;
}
}
}
// display on console word count
System.out.println("number of vowels in file = " + numOfVowels);
// print into new file
outputStream.write("number of vowels in file = " + "\n"
+ numOfVowels + "\n");
} catch (ArrayIndexOutOfBoundsException e) {
System.err.println("Caught " + "ArrayIndexOutOfBoundsException: "
+ e.getMessage());
} catch (IOException e) {
System.err.println("Caught IOException: " + e.getMessage());
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
}
public static void countPrepositions() throws IOException {
//what should I write in here? Am stuck
System.out.println("number of prepositions in the file =");
}
}
my input file says
Code :
Hello my name is bondgirl.
I am 21.
thanks again
Re: Counting Vowels and Prepositions in a text file
Quote:
Originally Posted by
JavaPF
Please show me the text file you will be using..
I probably wouldn't use the StreamTokenizer.
I would read in the file like this:
http://www.javaprogrammingforums.com...ner-class.html
And put each word into an array... You can then loop through the array to find you answers :)
Let me know if you need help and I will write an example..
yes could you please write an example..? thanks again