Results 1 to 5 of 5
- 01-16-2011, 01:20 AM #1
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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:
it works correctly, lists all the words in a file named output.java and they are stored in the words treeset.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); } }
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:
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.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; }
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?
- 01-16-2011, 01:43 AM #2
Senior Member
- Join Date
- Dec 2010
- Posts
- 100
- Rep Power
- 0
Hi sunde - if you are required to create a set of vowels, you can do so using the following contrustructor:
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:Set<Character> vowels = new TreeSet<Character>(); vowels.add('a'); vowels.add('A');
Just an idea, hope it helps :)Java Code:for(String s: words) { for(int i = 0; i < s.length(); i++) { if(vowels.contains(s.charAt(i))) { count something; } } count something; }
Best,--user0--
- 01-16-2011, 04:23 AM #3
Member
- Join Date
- Jan 2011
- Posts
- 18
- Rep Power
- 0
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() ) );
-
I have to wonder just what TextFile is? It must extend some Collection of some sort, perhaps an ArrayList.
- 01-18-2011, 07:22 AM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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:
Would this work? Also, if it is, a newly initiatied iterator, does it start at the 0 item in the list?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
Thanks very much for the help so far.
Similar Threads
-
Picking a Collection and Design for querying 1G Point objects
By ennuages in forum Advanced JavaReplies: 8Last Post: 11-22-2010, 10:09 AM -
Counting char in String
By j@v@ in forum New To JavaReplies: 2Last Post: 10-26-2010, 02:29 PM -
counting letters in a string
By beandip408 in forum New To JavaReplies: 12Last Post: 09-29-2010, 01:44 PM -
Find java objects in a given string.
By sarathi in forum New To JavaReplies: 3Last Post: 03-06-2009, 08:42 AM -
iterating through a collection of objects
By Scotty Boy in forum New To JavaReplies: 0Last Post: 04-10-2008, 01:28 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks