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..

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 =");
	}
}

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();
}

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.

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.

am counting words that i have in a "input.txt" file on my computer...

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.

It did work, i changed it to this:

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 {
		FileReader fr = new FileReader(
		"G:\\Java 1302\\project\\FILEIO\\src\\input.txt");
		BufferedReader br = new BufferedReader(fr);

		int numOfVowels = 0;

		String line = br.readLine();
		while (line!=null) {

		  char [] ch = line.toCharArray();  
		    for (int i = 0; i <line.length(); i++) {
		        char c = line.charAt(i);
		        if (c=='a' || c=='e' || c=='i' || c=='o' || c=='u') {
		        	numOfVowels++;
		        
		          line = br.readLine();
		        }
		System.out.println("number of vowels in file = " + numOfVowels);
		    }
		    }
			}
	public static void countPrepositions() throws IOException {
		System.out.println("number of prepositions in the file =");
	}
}

and the output i got was

number of words in file = 10
number of vowels in file = 0
number of vowels in file = 1
Exception in thread "main" java.lang.NullPointerException
	at FileIO.countVowels(FileIO.java:39)
	at FileIO.main(FileIO.java:8)

Is my loop wrong? and why is it printing out twice ? thanks in advance

It prints twice because you put your print statement inside of a for loop that has two iterations.

oh ok...and how come it's printing the wrong number of vowels...any ideas how i would fix that? i'm so stuck

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 but after the for loop exits. You should also use print statements to debug ..

Edited 6 Years Ago by BestJewSinceJC: n/a

i did this now

public static void countVowels() throws IOException {
		FileReader fr = new FileReader(
		"G:\\Java 1302\\project\\FILEIO\\src\\input.txt");
		BufferedReader br = new BufferedReader(fr);

		int numOfVowels = 0;
		String line = br.readLine();
		while (line!=null) {
		  char [] ch = line.toCharArray();  
		    for (int i = 0; i <line.length(); i++) {
		        char c = line.charAt(i);
		        if (c=='a' || c=='e' || c=='i' || c=='o' || c=='u') {
		        	numOfVowels++;
		      }
		    
		System.out.println("number of vowels in file = " + numOfVowels);
		    
		}
		     line = br.readLine();
		    }
	}

but output is

number of words in file = 10
number of vowels in file = 0
number of vowels in file = 1
number of vowels in file = 1
number of vowels in file = 1
number of vowels in file = 2
number of vowels in file = 2
number of vowels in file = 2
number of vowels in file = 2
number of vowels in file = 2
number of vowels in file = 2
number of vowels in file = 3
number of vowels in file = 3
number of vowels in file = 4
number of vowels in file = 4
number of vowels in file = 5
number of vowels in file = 5
number of vowels in file = 5
number of vowels in file = 6
number of vowels in file = 6
number of vowels in file = 7
number of vowels in file = 7
number of vowels in file = 8
number of vowels in file = 8
number of vowels in file = 9
number of vowels in file = 9
number of vowels in file = 10
number of prepositions in the file =

so am not understanding it.....

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.

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.

no any thing "ahahshd aljssla" that is two words
or "hi my i am here" that is 5 words
yes separated by spaces

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.

Ok i changed it, it works thanks......
do you know how or the outline i can use to count prepositions?

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.

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.

A preposition links nouns, pronouns and phrases to other words in a sentence. The word or phrase that the preposition introduces is called the object of the preposition.

A preposition usually indicates the temporal, spatial or logical relationship of its object to the rest of the sentence as in the following examples:

The book is on the table.
The book is beneath the table.
The book is leaning against the table.
The book is beside the table.
She held the book over the table.
She read the book during class.

In each of the preceding sentences, a preposition locates the noun "book" in space or in time.

would i have to write down all of them and check them like i did the vowels....
also i changed the print for the vowels not to be in the vowels for loop by it still prints twice....what's the problem? and the word count isn't printing to the output file..help..!

this is what i've done

public static void countWords() throws IOException {
                FileReader inputStream = null;
                FileWriter outputStream = null;
                String fileContents = "G:\\Java 1302\\project\\FILEIO\\src\\output.txt";
                
                try {
                        inputStream = new FileReader(
                                        "G:\\Java 1302\\project\\FILEIO\\src\\input.txt");
                        outputStream = new FileWriter(
                                        "G:\\Java 1302\\project\\FILEIO\\src\\ouput.txt");

                        BufferedReader br = new BufferedReader(inputStream);
                        String lineRead = br.readLine();
                        String wdcnt = " ";
                        while (lineRead != null) {
                                fileContents = fileContents + "\n" + lineRead;
                                lineRead = br.readLine();
                                // count words and display the count
                                String[] wordList = fileContents.split("[\\s\\n]");
                                // int size = wordList.length;
                                wdcnt = (" the total word count is " + (wordList.length - 2));
                        }
                        // display on console word count
                        System.out.println(wdcnt);

                        // print into new file
                        outputStream.write(wdcnt);
                } finally {
                        if (inputStream != null) {
                                inputStream.close();
                        }
                        if (outputStream != null) {
                                outputStream.close();
                        }
                }
        }
        public static void countVowels() throws IOException {
                FileReader inputStream = null;
                FileWriter outputStream = null;
                String fileContents = "G:\\Java 1302\\project\\FILEIO\\src\\output.txt";
                try {
                        inputStream = new FileReader(
                                        "G:\\Java 1302\\project\\FILEIO\\src\\input.txt");
                        outputStream = new FileWriter(
                                        "G:\\Java 1302\\project\\FILEIO\\src\\ouput.txt");

                        BufferedReader br = new BufferedReader(inputStream);
                        int numOfVowels = 0;
                        String lineRead = br.readLine();
                      
                        while (lineRead != null) {
                        	
                               // char[] ch = lineRead.toCharArray();
                                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
                                System.out.println("number of vowels in file: " + numOfVowels);
                                // print into new file                                
                                outputStream.write("number of vowels in file = "    + numOfVowels);
                                lineRead = br.readLine();
                        }    
                }      
                 finally {
                        if (inputStream != null) {
                                inputStream.close();
                        }
                        if (outputStream != null) {
                                outputStream.close();
                        }
                }
        }

you say to take the print statement out of the loop and put it where?, i've tried, it gives me errors or doesn't print....

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!");
}

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!");
}

ok thanks..am sorry to sound like a broken record but where do i put this

//display on console
                                System.out.println("number of vowels in file: " + numOfVowels);

so that it prints on console only once????

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?

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?

I know how they work generally, but when i do that, nothing prints or writes...

this is what i've done, after this then i don't understand...

while (lineRead != 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++;
										
					}
				}
							
			}
			///THIS IS WHERE I"VE PUT THEM PBUT THEY AREN"T PRINTING OR WRITING
			System.out.println("number of vowels in file: " + numOfVowels);
			// print into new file
			outputStream.write("number of vowels in file = " + numOfVowels);
			lineRead = br.readLine();
		
			
		} finally {
			if (inputStream != null) {
				inputStream.close();

IT worked now ..arrrrrrrrragh...THanks everyone you have been patient and understanding.

bondgirl, if you attach all of your current code as one post, I'll take a look at it for you.

thanks i worked , i'm working on counting prepositions...do u know a basic outline i could use for that?
this is my vowel one, can i manipulate it to count prepositions?

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++;
					}
				}
			}
This question has already been answered. Start a new discussion instead.