Results 1 to 5 of 5
  1. #1
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Counting vowles in a collection of String objects

    I am working on an exercise in Thinking in java 4th edition, I have an idea of how to do it, but I am not sure how to do it the way the book specifies.
    the question is:

    Create a set of vowels. working from uniquewords.java, count and display the number of vowels in each input word and also display the total number of vowels in the input file:

    the code for uniquewords.java is:
    Java Code:
    import java.util.*;
    import net.mindview.util.*;
    
    public class UniqueWords{
    	public static void main(String[] args){
    		Set<String> words = new TreeSet<String>(new TextFile("SetOperations.java", "\\W+"));
    		System.out.println(words);
    	}
    }
    it works correctly, lists all the words in a file named output.java and they are stored in the words treeset.

    I am a little confused still with all the collection types, and I am still trying to figure them all out.

    Here is my thought:
    Java Code:
    public static int countString(String str)
    {
      int count = 0;
      for(int i = 0; i < str.length(); i++) 
      {
        switch(str.charAt(i)){
          case 'a':
          case 'A':
          case 'e':
          case 'E':
          case 'i':
          case 'I':
          case 'o':
          case 'O':
          case 'u':
          case 'U':
              count += 1;
          default:
              count;
           }
        }
    return count;
    }
    then simply loop through the treeSet containing the words in a file applying each i to this new function. However, the book mentions using a Set of vowels, and Im not sure how I would implement them, also, would it be smarter to use an iterator to go through the treeSet? I am still a little weak with iterators as well.

    Also, I can easily figure out the vowel per word by creating a new set if Integers and storing the value of countVowels each word in the new set, is the correct way to think?

  2. #2
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    Hi sunde - if you are required to create a set of vowels, you can do so using the following contrustructor:
    Java Code:
    Set<Character> vowels = new TreeSet<Character>();
    vowels.add('a');
    vowels.add('A');
    After that you can have two counters: one to count vowels in each word, and one to count total vowels. You can use a for each loop to get each string word from your set and use the contains method of the Set object:
    Java Code:
    for(String s: words) {
      for(int i = 0; i < s.length(); i++) {
          if(vowels.contains(s.charAt(i)))  {
             count something;
          }
      }
      count something;
    }
    Just an idea, hope it helps :)
    Best,
    --user0--

  3. #3
    charleyjoyce is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    also display the total number of vowels in the input file:
    you can also read the whole file (if its not big) , and do this, assuming myString is the whole file already stored in a string.
    Java Code:
            String myString = "The quick brown fox jumped over the lazy dog";
            int totalLen = myString.length();
            System.out.println ("Total vowels: " + ( totalLen - myString.replaceAll("[aeiouAEIOU]","").length() ) );

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The textfile item is something the book creator created, he moves a bit ahead of what has been discusses so far. I have another question, if I want to use an iterator I am wondering if this is the correct method:
    Java Code:
    Iterator<String> it = ts.iterator();
    
    while(it.hasNext()){
      String p = it.next();
      for(int i = 0; i < p.length(); i++){
        count collection items
    Would this work? Also, if it is, a newly initiatied iterator, does it start at the 0 item in the list?

    Thanks very much for the help so far.

Similar Threads

  1. Replies: 8
    Last Post: 11-22-2010, 11:09 AM
  2. Counting char in String
    By j@v@ in forum New To Java
    Replies: 2
    Last Post: 10-26-2010, 03:29 PM
  3. counting letters in a string
    By beandip408 in forum New To Java
    Replies: 12
    Last Post: 09-29-2010, 02:44 PM
  4. Find java objects in a given string.
    By sarathi in forum New To Java
    Replies: 3
    Last Post: 03-06-2009, 09:42 AM
  5. iterating through a collection of objects
    By Scotty Boy in forum New To Java
    Replies: 0
    Last Post: 04-10-2008, 02:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •