Results 1 to 4 of 4
- 11-07-2010, 08:34 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 15
- Rep Power
- 0
counting the vowels in a sentence
here is the problem:
Write a program called ArrayVowels.java. This program has a countVowel method that
accepts a String as a parameter and produces and returns an array of integers representing the
counts of each vowel in the string. The array returned by your program should hold five
elements: the first is the count of As, the second is the count of Es, the third is the count of Is,
the fourth is the count of Os, the fifth is the count of Us. Your method will use case
insensitive comparisons. For example, the call countVowel(“I think, therefore, I am”) should
return the array {1, 3, 3, 1, 0}.
In your main method, call the countVowel method to test its correctness. You can either
design some good test cases and hard-code the countVowel method calls, or use Scanner
object to read the string from the console.
here is the code i have so far
I thought this should've worked but it gave me errors:Java Code:public class ArrayVowels { public static void main (String args[]) { String sentence = ("I think, therefore, I am"); } public static void countVowel(String sentence){ int[] counts = new int[5]; for (int i = 0; i < sentence.length(); i++) { char c = sentence.charAt(i); if (c == 'a') { counts[0]++; } else if (c == 'e') { counts[1]++; } else if (c == 'i') { counts[2]++; } else if (c == 'o') { counts[3]++; } else if (c == 'u') { counts[4]++; } } } System.out.println(Arrays.toString(counts)); }
First off what do these errors mean? Second how do I fix this, what am I doing wrong? thanks!Java Code:ArrayVowels.java:25: <identifier> expected System.out.println(Arrays.toString(counts)); ^ ArrayVowels.java:25: <identifier> expected System.out.println(Arrays.toString(counts));
-
You will either need to import java.util.Arrays or call it with its fully qualified name.
- 11-07-2010, 09:53 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 15
- Rep Power
- 0
I just added the import java.util.*
and its giving me the same errors.
- 11-08-2010, 06:31 AM #4
Your System.out.println(Arrays.toString(counts)); call is not actually in a method.
Similar Threads
-
please help with counting specific word in a sentence
By noobinoo in forum New To JavaReplies: 4Last Post: 05-07-2010, 02:06 PM -
Replacing Vowels in a word.
By mklprasad in forum Advanced JavaReplies: 1Last Post: 10-05-2009, 12:31 PM -
enter a string sentence
By amorosa19 in forum New To JavaReplies: 11Last Post: 01-28-2009, 04:05 AM -
Counting Vowels and Constonants
By MattN in forum New To JavaReplies: 3Last Post: 11-20-2007, 05:45 PM -
How to extract info from a sentence
By luisarca in forum XMLReplies: 1Last Post: 06-07-2007, 05:43 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks