How to write a static method for an array list.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SLDenman
    New Member
    • Sep 2010
    • 4

    How to write a static method for an array list.

    I have created a vowel counting program and I'm not sure how to write the static method that accepts the String as a parameter. Can anyone help me write it?

    Code:
    //import statement for ArrayList
    import java.util.ArrayList;
    	
    public class countingVowels {
    
    
    	public class VowelsProgram 
    	{
    		public static void main(String args[])
    		{
    			//create ArrayList that stores String object
    			ArrayList actor = new ArrayList();
    			
    			//add Indian actor names to above ArrayList
    			actor.add("Amitabh Bachan");
    			actor.add("Rajnikanth");
    			actor.add("Kamal Hassan");
    			actor.add("Shahrukh Khan");
    			actor.add("Akshay Kumar");
    			
    			//Write a loop statement that calls countingVowels method for each item in the ArrayList
    			//and prints number of occurrence of vowels in the item.
    			 int aCounter = 0;
    			 int eCounter = 0;
    			 int iCounter = 0;
    			 int oCounter = 0;
    			 int uCounter = 0;
    			 
    			 
    			 String line, temp;
    			 int letter;
    
    			 for (int i=0; i<=letter; i++)
    
    			if (line.charAt(i) == 'a')
    				 aCounter++;
    				
    				else if (line.charAt(i) == 'e')
    				 eCounter++;
    				
    				else if (line.charAt(i) == 'i')
    				 iCounter++;
    				
    				else if (line.charAt(i) == 'o')
    				 oCounter++;
    
    				System.out.println ("Number of 'a': " +aCounter);
    				System.out.println ("Number of 'e': " +eCounter);
    				System.out.println ("Number of 'i': " +iCounter);
    				System.out.println ("Number of 'o': " +oCounter);
    				System.out.println ("Number of 'u': " +uCounter);
    			
    				
    		}
    
    		//Write a static countingVowels method that accepts String as parameter.
    		//this method returns integer array contains count of vowels character
    		//occurrence in the parameter.
    
    return ;
    	}
    
    }
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    You have following code posted.
    Code:
    int letter;
    for (int i=0; i<=letter; i++)
    You have letter declared but where is the initialization? What value does letter have?

    Similar story with variable line of type String

    Code:
    String line, temp;
    if (line.charAt(i) == 'a')
          aCounter++;
    There will be no String literal gets stored in the variable line.

    Regards
    Dheeraj Joshi

    Comment

    Working...