You don't need the StringTokenizer:
BufferedReader br = new BufferedReader(fr);
int numOfVowels = 0;
String line = br.readLine();
while (line!=null) {
char [] ch = line.toCharArray();
// loop the char array.
// for each character in the array, if you find a vowel increase the numOfVowels
// always the last command to get the next line, before checking again at the begining of the loop if it is not null
line = br.readLine();
}
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
How do you plan to count the number of words? Do you mean, like, words that would appear in the dictionary? If that is the case then you are going to need to do a dictionary look up of each possible word from your input file. If you count a word as anything preceded and followed by spaces, then that's a different story.
BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
am counting words that i have in a "input.txt" file on my computer...
What do you consider to be a "word"? Does it have to be in a particular dictionary? Or is your file guaranteed to only contain dictionary words separated by spaces? You haven't explained your problem sufficiently.
BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
It prints twice because you put your print statement inside of a for loop that has two iterations.
BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
On line 44 you said line = br.readLine(); but that shouldn't be inside of that if statement. You want to read in the next line when you're done looking at the previous line; so you should call br.readLine() inside of your while loop butafter the for loop exits. You should also use print statements to debug ..
BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
You should only print out the number of vowels outside of the loops. Print out the number of vowels right before the end of the method.
BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
Ok i changed it, it works thanks......
do you know how or the outline i can use to count prepositions?
What are prepositions?
Anyway, since you have the each line, you can use as "skeleton" the code you have and process the line anyway you want.
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
bondgirl: For the prepositions you could add them all to an ArrayList and then use the contains() method to see if one was there. For example:
ArrayList<String> list = new ArrayList<String>();
list.add("on");
if (list.contains("beneath")){
System.out.println("The preposition is in the list! Do something!");
}
BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
Put it at the end of your method that calculates the number of vowels, but not inside of a loop. Don't you know how loops work?
BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354