Results 1 to 3 of 3
  1. #1
    SMHouston is offline Member
    Join Date
    Sep 2009
    Posts
    80
    Rep Power
    0

    Default Is there a way to clean up this recursion code?

    Ok, so I wrote a program to count the number of vowels in a user inputted string.

    The program works great. However, I was wondering if there was a way to clean my recursion method up, trying to get things as simple as possible if this isn't it. Need to work on cleaning my code up.

    Here is the program:

    Java Code:
    //Program to test vowel count
    
    import java.io.*;
    import java.util.*;
    
    public class VowelCount
    {
    	static BufferedReader keyboard =
    				new BufferedReader(new InputStreamReader(System.in));
    	
    	static int countVowels(String s) 
    	{
    		if (s.length() == 0) return 0;
    		int tailResult = countVowels(s.substring(1));
    		switch (s.charAt(0)) 
    		{
    			case 'a': case 'A':
    			case 'e': case 'E':
    			case 'i': case 'I':
    			case 'o': case 'O':
    			case 'u': case 'U':
    					
    			return tailResult+1;
    					default:
    						return tailResult;
    		}
    	}
    	
    	public static void main(String[] args) throws IOException
    	{
    	
    		String s;
    		
    		System.out.println("Please enter a string of letters: ");
    		s = keyboard.readLine();
    		System.out.println("The string you entered is: " + s);
    		countVowels(s);
    		System.out.println("The number of vowels in the string is: " + countVowels(s));
    	}
    }
    Any advice is much appreciated.

  2. #2
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    There are several ways to do this.

    Method 1: For each character in the string, check if it's a vowel, and keep a running total.

    Java Code:
    static int countVowels(String s) 
    {
    	int vowelCount = 0;
    
    	for (int i = 0; i < s.length(); i++)
    	{
    		char c = s.charAt(i);
    		
    		if (c == 'a' || c == 'e' || c == 'i' || c=='o' || c == 'u'
    		|| c == 'A' || c == 'E' || c == 'I' || c =='O' || c=='U')
    		{
    			vowelCount++;
    		}
    	}
    		
    	return vowelCount;
    }
    This method differs from yours in that instead of recursively checking the end of the string, you check each character.


    Method 2: Use a regular expression.

    I'm a regex maniac, and use it for most of my parsing needs, since it's usually shorter code, and easier to modify.

    Java Code:
    static int countVowels(String s)
    {
    	int vowelCount = 0;
    
    	// matches an 'a', 'e', 'i', 'o' or 'u'
    	// (the "(?i)" means case-insensitive (that is, upper-case vowels too)
    	Pattern vowel = Pattern.compile("(?i)[aeiou]");
    	Matcher matcher = vowel.matcher(s);
    
    	while (matcher.find()) {
    		vowelCount++;
    	}
    
    	return vowelCount;
    }

    Also, as a note, this line does nothing, since you don't store the value.
    Java Code:
    countVowels(s);

    Instead, you might want to store the value.
    Java Code:
    int vowelCount = countVowels(s);
    System.out.println("The number of vowels in the string is: " + vowelCount);
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  3. #3
    SMHouston is offline Member
    Join Date
    Sep 2009
    Posts
    80
    Rep Power
    0

Similar Threads

  1. [REQ] could anybody clean this method up for me?
    By harryblue in forum Advanced Java
    Replies: 5
    Last Post: 03-19-2009, 02:37 AM
  2. [REQ] can sombody clean up this code?
    By harryblue in forum New To Java
    Replies: 1
    Last Post: 03-18-2009, 08:40 PM
  3. Recursion
    By Mika in forum New To Java
    Replies: 5
    Last Post: 01-04-2009, 01:13 AM
  4. clean and Build
    By bhanu in forum Eclipse
    Replies: 3
    Last Post: 07-03-2008, 01:13 PM
  5. Clean the content of the JTextField
    By elizabeth in forum AWT / Swing
    Replies: 1
    Last Post: 07-26-2007, 08:38 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
  •