Results 1 to 12 of 12
  1. #1
    Slick Java is offline Member
    Join Date
    Dec 2012
    Posts
    11
    Blog Entries
    1
    Rep Power
    0

    Default Homework help - pairs of vowels to asterisks

    Hello, I have a homework assignment on changing pair of vowels to asterisks.
    I have so far achieved changing a single vowel into a asterisk.
    I need help on changing pairs of vowels into asterisks.
    eg. bookkeeping => b**k**ping not b**kk**p*ng
    /\

    Output (single vowel):

    Type in a word to protect
    bookkeeping
    b**kk**p*ing


    Java Code:
    import java.util.*;
    public class HomeworkV03 {
    	
    	private String chars;
    	private Scanner scan = new Scanner(System.in);
    	private char[] vowels = {'a','e','i','o','u'};
    	
    	
    	
    	public static void main(String args[])
    	{
    		HomeworkV03 h = new HomeworkV03();
    		h.askClient();
    		h.printVowels();
    	}
    	
    	public void askClient()
    	{
    		System.out.println("Type in a word to protect");
    		chars = scan.next();
    	}
    	
    	public void printVowels()
    	{
    		int a = 0;
    		char c;
    
    		
    		for (a = 0; a < chars.length(); a++)
    		{
    				
    			
    			    c = chars.charAt(a);
    			    boolean isVowel = isVowel(c);
    			    
    
    			   
    			    if(isVowel )
    			    {
    			    	
    			    	System.out.print("*");
    			    	
    			    }
    			    else
    			    {
    			    	System.out.print(c);
    			    }
    			
    		  }
    		
    		}
    
    	private boolean isVowel(char c) 
    	{
    		int i = 0;
    		boolean vowel = false;
    		
    		
    		for (i = 0; i < 5; i++)
    		{	
    			    if(c == vowels[i])
    			 	{
    					vowel = true;
    				}	
    	
    		}
    			
    		return vowel;
    	}
    }
    Thanks,

    Slick Java
    Last edited by Slick Java; 03-14-2013 at 10:02 AM. Reason: Not enough imformation

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Homework help - pairs of vowels to asterisks

    Your code doesn't compile.
    eg. bookkeeping => b**k**ping not b**k**p*ng
    What happened to the second k ?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    Slick Java is offline Member
    Join Date
    Dec 2012
    Posts
    11
    Blog Entries
    1
    Rep Power
    0

    Default Re: Homework help - pairs of vowels to asterisks

    woops. Forgot to type that in. Ill edit it.

  4. #4
    Slick Java is offline Member
    Join Date
    Dec 2012
    Posts
    11
    Blog Entries
    1
    Rep Power
    0

    Default Re: Homework help - pairs of vowels to asterisks

    The program compiles on eclipse. I don't see why it can't.

  5. #5
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Homework help - pairs of vowels to asterisks

    My bad, your bad naming isVowel vs. isVowel() ;)
    You have to either look ahead at the next char or look back at the preivious char and compare them. Another option would be to use a regex to replace all vowel pairs.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  6. #6
    Nouish is offline Member
    Join Date
    Dec 2012
    Posts
    12
    Rep Power
    0

    Default Re: Homework help - pairs of vowels to asterisks

    1. When checking your array of characters to determine whether a given character is a vowel or not, there is no reason to continue after a positive match:

    Java Code:
    private boolean isVowel(char character)
    {
    	for (char c : vowels)
    	{
    		if (c == character)
    		{
    			return true;
    		}
    	}
    
    	return false;
    }
    2. Can you please submit an example of the output you want to have? Do you want to remove "aa" or "oo", but leave the third "o" in input "ooo"?

    3. Out of interest, isn't Y a vowel in the English language? I believe it is in my language :p

  7. #7
    Slick Java is offline Member
    Join Date
    Dec 2012
    Posts
    11
    Blog Entries
    1
    Rep Power
    0

    Default Re: Homework help - pairs of vowels to asterisks

    Quote Originally Posted by Nouish View Post
    1. When checking your array of characters to determine whether a given character is a vowel or not, there is no reason to continue after a positive match:

    Java Code:
    private boolean isVowel(char character)
    {
    	for (char c : vowels)
    	{
    		if (c == character)
    		{
    			return true;
    		}
    	}
    
    	return false;
    }
    2. Can you please submit an example of the output you want to have? Do you want to remove "aa" or "oo", but leave the third "o" in input "ooo"?

    3. Out of interest, isn't Y a vowel in the English language? I believe it is in my language :p
    Type in a word to protect
    bookkeeping
    b**kk**ping

    NOT

    Type in a word to protect
    bookkeeping
    b**kk**p*ng

  8. #8
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Homework help - pairs of vowels to asterisks

    The solution is in my reply #5. Does it work? What have you changed?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default Re: Homework help - pairs of vowels to asterisks

    Quote Originally Posted by Nouish View Post
    3. Out of interest, isn't Y a vowel in the English language? I believe it is in my language :p
    No, it's a 'diphtong' or 'gliding vowel'; you have to change your lips and/or mouth position to pronounce it; not so with vowels.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    Slick Java is offline Member
    Join Date
    Dec 2012
    Posts
    11
    Blog Entries
    1
    Rep Power
    0

    Default Re: Homework help - pairs of vowels to asterisks

    Quote Originally Posted by PhHein View Post
    The solution is in my reply #5. Does it work? What have you changed?
    Can you give me an example of what you mean in reply 5?

  11. #11
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: Homework help - pairs of vowels to asterisks

    I think he meant using regex.... but be careful if you do....

    Java Code:
         out = in.replaceAll("[a,e,i,o,u,A,E,I,O,U]{2}+", "**");
    Will replace only pairs... "Beautiful" will break it -> "B**utiful"

    Not to mention if this is a beginning programming class the use of regular expressions may raise a few flags.... Speaking of flags..

    Java Code:
        boolean lastCharVowel = false;
    
    ....
    
        if(isVowel(c) && (lastCharVowel || (a+1 < chars.length && isVowel(chars.charAt(a+1)))){
              // OK, so that's ugly... but gets the point across
              System.out.print("*");
             lastCharVowel = true;
        }
        else{    // the fact that we looked ahead... can just set the lastCharVowel back to false whether or not the current char is a vowel
                   // b/c there aren't two in a row.  Using the boolean variable as a tracker allows for multiple vowels in a row.
    ....
    Probably want the logic to be a little more clear... so

    Java Code:
    IF at a vowel
    	IF last character was a vowel
    		THEN print "*"
    	ELSE IF there are more characters in the word 
    		THEN IF next char is a vowel
    			THEN set last char flag and print "*"
    		ELSE not at vowel
    	ELSE not at vowel
    ELSE not at vowel
    Last edited by SJF; 03-16-2013 at 04:34 AM. Reason: formatting

  12. #12
    Slick Java is offline Member
    Join Date
    Dec 2012
    Posts
    11
    Blog Entries
    1
    Rep Power
    0

    Default Re: Homework help - pairs of vowels to asterisks

    Ok, Ill try that!

Similar Threads

  1. Vowels
    By hockey101 in forum New To Java
    Replies: 28
    Last Post: 12-02-2012, 07:20 PM
  2. Counting Vowels, getting an error
    By gabrielpr12 in forum New To Java
    Replies: 8
    Last Post: 01-07-2012, 07:50 PM
  3. Box of asterisks
    By golf_pro90 in forum New To Java
    Replies: 6
    Last Post: 11-01-2011, 10:53 PM
  4. Replacing Vowels in a word.
    By mklprasad in forum Advanced Java
    Replies: 1
    Last Post: 10-05-2009, 12:31 PM
  5. Counting Vowels and Constonants
    By MattN in forum New To Java
    Replies: 3
    Last Post: 11-20-2007, 05:45 PM

Tags for this Thread

Posting Permissions

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