Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Counting Vowels and Prepositions in a text file

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

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

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


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default 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..
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Junior Member
    Join Date
    Apr 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Post Re: Counting Vowels and Prepositions in a text file

    Quote Originally Posted by JavaPF View Post
    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

    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
    Hello my name is bondgirl.
    I am 21.

    thanks again

  4. #4
    Junior Member
    Join Date
    Apr 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Post Re: Counting Vowels and Prepositions in a text file

    Quote Originally Posted by JavaPF View Post
    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

Similar Threads

  1. counting words of a text file
    By maybach230 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 6th, 2010, 03:40 PM
  2. java program to copy a text file to onother text file
    By francoc in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 23rd, 2010, 03:10 PM
  3. Read data from text file
    By yroll in forum Algorithms & Recursion
    Replies: 4
    Last Post: December 31st, 2009, 12:40 AM
  4. write text to a file help
    By wolfgar in forum File I/O & Other I/O Streams
    Replies: 8
    Last Post: November 24th, 2009, 07:36 AM
  5. Java program to reduce spaces between the words in a text file
    By tyolu in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: May 13th, 2009, 07:17 AM