Results 1 to 4 of 4
  1. #1
    huntrguy102 is offline Member
    Join Date
    Oct 2010
    Posts
    15
    Rep Power
    0

    Default 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

    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));
    	
    }
    I thought this should've worked but it gave me errors:
    Java Code:
    ArrayVowels.java:25: <identifier> expected
    	System.out.println(Arrays.toString(counts));
    	                  ^
    ArrayVowels.java:25: <identifier> expected
    	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!

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

  3. #3
    huntrguy102 is offline Member
    Join Date
    Oct 2010
    Posts
    15
    Rep Power
    0

    Default

    I just added the import java.util.*

    and its giving me the same errors.

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Your System.out.println(Arrays.toString(counts)); call is not actually in a method.

Similar Threads

  1. enter a string sentence
    By amorosa19 in forum New To Java
    Replies: 13
    Last Post: 09-06-2013, 11:34 AM
  2. Replies: 4
    Last Post: 05-07-2010, 03:06 PM
  3. Replacing Vowels in a word.
    By mklprasad in forum Advanced Java
    Replies: 1
    Last Post: 10-05-2009, 01:31 PM
  4. Counting Vowels and Constonants
    By MattN in forum New To Java
    Replies: 3
    Last Post: 11-20-2007, 06:45 PM
  5. How to extract info from a sentence
    By luisarca in forum XML
    Replies: 1
    Last Post: 06-07-2007, 06:43 PM

Posting Permissions

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