Help

Discussion in 'Programming General' started by wtp, Apr 23, 2011.

Help
  1. Unread #1 - Apr 23, 2011 at 5:12 PM
  2. wtp
    Joined:
    Nov 25, 2005
    Posts:
    2,455
    Referrals:
    2
    Sythe Gold:
    0

    wtp Grand Master
    Banned

    Help

    [​IMG]

    I'm trying to do the second problem there. What would I have to do to make it check if it's a vowel or not?
     
  3. Unread #2 - Apr 24, 2011 at 12:30 AM
  4. Fate1
    Joined:
    Apr 21, 2005
    Posts:
    773
    Referrals:
    2
    Sythe Gold:
    5

    Fate1 Apprentice
    Banned

    Help

    Write a method that takes an int i as a parameter and run it through a loop that iterates through the string at index i. Then use an if statement to see if the String at index i equals a vowel. If so return true if not return false.
     
  5. Unread #3 - Apr 24, 2011 at 10:35 AM
  6. Pottworth
    Joined:
    Nov 27, 2005
    Posts:
    83
    Referrals:
    0
    Sythe Gold:
    0

    Pottworth Member

    Help

    The String class in Java has the method - charAt(int i)

    You can use that in the if statement to see if it equals a vowel. If so, increment the vowel counter.

    Something like this;

    if(input.charAt(i) == 'a' || input.charAt(i) == 'e' ... i, o, u)
    {
    increment the vowel counter...
    increment the index variable...
    }
     
  7. Unread #4 - Apr 24, 2011 at 10:37 PM
  8. wtp
    Joined:
    Nov 25, 2005
    Posts:
    2,455
    Referrals:
    2
    Sythe Gold:
    0

    wtp Grand Master
    Banned

    Help

    Thanks guys for the help. I really appreciate it. I'm almost there, but I'm not sure how to make it loop correctly. I'm not getting any errors.

    Code:
    import java.util.Scanner;
    
    public class two
    {
    	static double vowel;
    	static String word;
    	static String word2;
    	static int i = 0;
    	static int count = -1;
    
    	public static void main(String[] args)
    	{
    		Scanner keyboard = new Scanner(System.in);
    
    		System.out.println("Enter charcter(s)");
    			word = keyboard.nextLine();
    
    		isVowel(word);
    
    		System.out.println("There are/is " + vowel +
    						    " vowel(s) in the entry: "
    						    + word);
    	}
    
    	public static void isVowel(String word2)
    	{
    		while(count < i)
    		{
    
    		if(word2.charAt(i) == 'a' || word2.charAt(i) == 'e' ||
    		   word2.charAt(i) == 'i' || word2.charAt(i) == 'o' ||
    		   word2.charAt(i) == 'u')
    		   	{
    		   		vowel++;
    		   		i++;
    		   		count++;
    		   	}
    		else
    				i++;
    				count++;
    		}
    	}
    }
    
    Output:

    Enter charcter(s)
    aio
    There are/is 1.0 vowel(s) in the entry: aio
     
  9. Unread #5 - Apr 25, 2011 at 8:56 AM
  10. Fate1
    Joined:
    Apr 21, 2005
    Posts:
    773
    Referrals:
    2
    Sythe Gold:
    5

    Fate1 Apprentice
    Banned

    Help

    You should do

    Code:
    while(count < word2.length()) {
    
    
    }
    That will iterate through the entire string instead of just the first letter.
     
  11. Unread #6 - Apr 25, 2011 at 3:00 PM
  12. wtp
    Joined:
    Nov 25, 2005
    Posts:
    2,455
    Referrals:
    2
    Sythe Gold:
    0

    wtp Grand Master
    Banned

    Help

    It's giving me an error when I do that:

    cannot find symbol
    symbol : variable length
    location: class java.lang.String
    while(count < word2.length)
    ^
     
  13. Unread #7 - Apr 25, 2011 at 3:43 PM
  14. Fate1
    Joined:
    Apr 21, 2005
    Posts:
    773
    Referrals:
    2
    Sythe Gold:
    5

    Fate1 Apprentice
    Banned

    Help

    Oh sorry its .length(); not just .length;
     
< Windows API Question | Buying a easy bot/program (up to 100m gp payment) >

Users viewing this thread
1 guest


 
 
Adblock breaks this site