Page 1 of 7 123 ... Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    39
    Rep Power
    2

    Alternating vowels and consonants


    I am working on a project and one of the questions is "create a program that can take in user input and tell if it composes of alternating vowels and consonants, example rehab." i only have 1 more day and this is the only question im stuck on. I am a beginner at java.
    this is the code i have so far:
    import javax.swing.JOptionPane;


    public class AlternatingVowelsAndConsonants
    {
    public static void main(String[] args)

    {




    String vowels = "aeiou";

    String consonants = "bcdfghjklmnpqrstvwxyz";

    String userInput;

    boolean isAlternating = false;


    userInput = JOptionPane.showInputDialog(null, "Enter a word ");

    String strippedInput = userInput.replaceAll("\\W", "");

    String strippedInput1 = strippedInput.toLowerCase();

    if (userInput.equals(""))
    {
    JOptionPane.showMessageDialog(null, "Invalid. Input is required.");
    }

    else
    {
    // checking if first letter is a vowel

    if (vowels.indexOf(strippedInput1.substring(0,1)) != -1)
    {
    for (int i = 0; i < strippedInput.length(); i++)
    {
    consonants.indexOf(i+1);
    vowels.indexOf(i);
    isAlternating = true;

    }
    }
    // checking if first letter is a consonant

    else if (consonants.indexOf(strippedInput1.substring(0,1)) != -1)
    {
    for (int i = 0; i < strippedInput.length(); i++)
    {
    consonants.indexOf(i);
    vowels.indexOf(i+1);
    isAlternating = true;


    }
    }

    else

    isAlternating = false;

    }

    if (isAlternating = true)

    JOptionPane.showMessageDialog(null,"word is valid");

    else

    JOptionPane.showMessageDialog(null,"word is not valid");

    }

    }

  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    47
    Rep Power
    0
    Code:
    try {
    	for(int z=0;z<s.length();z++){
    		if(isConsonant(s.charAt(z))&& isConsonant(s.charAt(z+1))) {
    			System.out.println(s+"    is   "+" NOT alternating");boo=false;break;
    	
    		}
    		
    		if(!isConsonant(s.charAt(z))&& !isConsonant(s.charAt(z+1))) {
    			System.out.println(s+"    is   "+" NOT alternating");boo=false;break;
    	
    		}
    	}
    } catch (Exception e) {
    	// TODO Auto-generated catch block
    	//e.printStackTrace();
    }
    In your code you'll need such piece of code
  4. #3
  5. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    You can't say the String follows the rules (isAlternating = true until you have looked at all the letters. You can say it is not following the rules as soon as you find the first violation of the rules. When you find a violation, there is no reason to look at any more data and can immediately say it doesn't follow the rules. The break statement could be used to exit the loop at that point.


    Please edit the post and wrap the code in code tags
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    47
    Rep Power
    0
    Originally Posted by ctroop4ever
    I am working on a project and one of the questions is "create a program that can take in user input and tell if it composes of alternating vowels and consonants, example rehab." i only have 1 more day and this is the only question im stuck on. I am a beginner at java.
    this is the code i have so far:
    import javax.swing.JOptionPane;


    public class AlternatingVowelsAndConsonants
    {
    public static void main(String[] args)

    {




    String vowels = "aeiou";

    String consonants = "bcdfghjklmnpqrstvwxyz";

    String userInput;

    boolean isAlternating = false;


    userInput = JOptionPane.showInputDialog(null, "Enter a word ");

    String strippedInput = userInput.replaceAll("\\W", "");

    String strippedInput1 = strippedInput.toLowerCase();

    if (userInput.equals(""))
    {
    JOptionPane.showMessageDialog(null, "Invalid. Input is required.");
    }

    else
    {
    // checking if first letter is a vowel

    if (vowels.indexOf(strippedInput1.substring(0,1)) != -1)
    {
    for (int i = 0; i < strippedInput.length(); i++)
    {
    consonants.indexOf(i+1);
    vowels.indexOf(i);
    isAlternating = true;

    }
    }
    // checking if first letter is a consonant

    else if (consonants.indexOf(strippedInput1.substring(0,1)) != -1)
    {
    for (int i = 0; i < strippedInput.length(); i++)
    {
    consonants.indexOf(i);
    vowels.indexOf(i+1);
    isAlternating = true;


    }
    }

    else

    isAlternating = false;

    }

    if (isAlternating = true)

    JOptionPane.showMessageDialog(null,"word is valid");

    else

    JOptionPane.showMessageDialog(null,"word is not valid");

    }

    }


    Code:
    import java.util.Scanner;
    
    import javax.swing.JOptionPane;
    
    public class ScanningText {
    //static String s;static StringBuilder st=new StringBuilder("");static int vow=0,len =0;static int charNum,i;
    	public static void main(String[] args) {
    		  String s;  StringBuilder st=new StringBuilder("");  int vow=0,len =0;  int charNum,i;
    
    		
    		
    		int[] letters = new int[26];
            char lettersAlph[]=new char[26];
            int cnt[]=new int[26];
            for(i=0;i<26;i++)
            {
            lettersAlph[i]= (char) (65+i);	
            cnt[i]=0;	
          // System.out.println(lettersAlph[i]); 	
            }
            	   	
    		//s = JOptionPane.showInputDialog(
            
            System.out.println("Enter a string");
    		  s=new Scanner(System.in).nextLine();
    		 s=s.toUpperCase();
    		for( i=0;i<s.length();i++){
    		if(Character.isAlphabetic(s.charAt(i)))	st.append(s.charAt(i));
    			
    		}
    		s=st.toString();
    		 
            len=s.length();
    		for(int c=0; c<s.length(); c++) {
    	    charNum = s.charAt(c);
    	    for(int z=0;z<26;z++){
    		if(charNum==(char)(65+z))
    		cnt[z]+=1;
    	    }
    		}
    	    for(int z=0;z<len;z++){
    	    	charNum = s.charAt(z);
    	    	if(charNum=='A'||charNum=='E'||(charNum)=='I'||charNum=='O'||charNum=='U'||charNum=='İ')vow++;
    	    	
    
    	    
    	    
    	    
    	    }
    
    	    for(int c=0; c<s.length(); c++) {
    		    
    	for(i=0;i<26;i++)System.out.println(lettersAlph[i] +" = " + cnt[i]);
    	    }
    	System.out.println("There are    "+ vow +"   vowels");
    	System.out.println("There are    "+ (s.length()-vow) +"   consonants");
    	System.out.println(s);
    	int j=0,l=0;
    	for(int k=0;k<s.length();k++){
    		if(isVowel(s.charAt(k)))j++;
    		}
       
    
    	for(int k=0;k<s.length();k++){
    		if(isConsonant(s.charAt(k)))l++;
    		}
       
    	System.out.println("There are "+j+"  Vowels");
    	
    	System.out.println("There are "+l+"  Consonants");
    boolean boo=true;	
    try {
    	for(int z=0;z<s.length();z++){
    		if(isConsonant(s.charAt(z))&& isConsonant(s.charAt(z+1))) {
    			System.out.println(s+"    is   "+" NOT alternating");boo=false;break;
    	
    		}
    		
    		if(!isConsonant(s.charAt(z))&& !isConsonant(s.charAt(z+1))) {
    			System.out.println(s+"    is   "+" NOT alternating");boo=false;break;
    	
    		}
    	}
    } catch (Exception e) {
    	// TODO Auto-generated catch block
    	//e.printStackTrace();
    }
    if(boo)System.out.println(s+"    is   "+"   alternating");
    
    
    
    	/*if((j+l)%2==1){
    		
    		//JOptionPane.showMessageDialog(null, s+"    is   "+" alternating");
    		System.out.println(s+"    is   "+" alternating");
    	}
    	//else   JOptionPane.showMessageDialog(null, s+"    is  NOT  "+" alternating");
    	else {
    		System.out.println(s+"    is   "+" NOT alternating");	}}
    */
    	}
    static boolean isVowel(char charNum){
    	if(charNum=='A'||charNum=='E'||charNum=='I'||charNum=='O'||charNum=='U'||charNum=='İ')return true;
    	return false;
    }
    
    static boolean isConsonant(char charNum){
    	boolean bool=!isVowel(charNum);
    	if(bool)return true;
    	return false;
    }
    
    }

    Comments on this post

    • Aurum84 disagrees : Please stop providing complete answers. Rather provide snippets or hints.
    • NormR disagrees : Please don't do OPs work for him
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    39
    Rep Power
    2
    That code doesnt help me because I am not trying to count the number of vowels or consonants, i am simply just trying to check if a sentence or word consists of alternating vowels and consonants like CVCV CVCV or the other way around, and like I said I am a beginner and using JOptionPane, I don't use Scanner yet, and last of all I don't understand what that code means, thank you though and sorry for wasting your time
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Can you describe the simple steps to take to solve the problem?
    Work out the logic before trying to write the code.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    47
    Rep Power
    0
    Originally Posted by ctroop4ever
    That code doesnt help me because I am not trying to count the number of vowels or consonants, i am simply just trying to check if a sentence or word consists of alternating vowels and consonants like CVCV CVCV or the other way around, and like I said I am a beginner and using JOptionPane, I don't use Scanner yet, and last of all I don't understand what that code means, thank you though and sorry for wasting your time
    This checks vcvc... or cvcv...

    Code:
    	System.out.println(s+"    is   "+" alternating");
    	}
    	 s+"    is  NOT  "+" alternating");
    	else {
    		System.out.println(s+"    is   "+" NOT alternating");	}}
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    39
    Rep Power
    2
    Simple steps: 1. Get input 2. Check if the first letter is a vowel or not. If it is then set the next letter to be a consonant and somehow keep swapping like that? Or if it starts with a consonant then do the same but reverse. 3. If it is neither (not alternating) then break the for loop or whatever loop im using. 4. This has to apply to a word or sentence which is why I stripped it and took out the spaces and punctuation.

    I still havent figured out how to write the code and today is the last day
  16. #9
  17. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    somehow keep swapping like that?
    That's the part that needs to be described. What are the steps for the "somehow" part?
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    47
    Rep Power
    0
    Originally Posted by ctroop4ever
    Simple steps: 1. Get input 2. Check if the first letter is a vowel or not. If it is then set the next letter to be a consonant and somehow keep swapping like that? Or if it starts with a consonant then do the same but reverse. 3. If it is neither (not alternating) then break the for loop or whatever loop im using. 4. This has to apply to a word or sentence which is why I stripped it and took out the spaces and punctuation.

    I still havent figured out how to write the code and today is the last day
    In the code I sent you, ignore

    // the commented out code

    And without JOptionPane , you will achieve the program's goal
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    47
    Rep Power
    0
    Code:
    import java.util.Scanner;
    
    import javax.swing.JOptionPane;
    
    public class ScanningText {
     	public static void main(String[] args) {
    		  String s;  StringBuilder st=new StringBuilder("");  int vow=0,len =0;  int charNum,i;
    
    		
    		
    		int[] letters = new int[26];
            char lettersAlph[]=new char[26];
            int cnt[]=new int[26];
            for(i=0;i<26;i++)
            {
            lettersAlph[i]= (char) (65+i);	
            cnt[i]=0;	
             }
            	   	
             
             
            s=JOptionPane.showInputDialog("Enter a string");
    		 s=s.toUpperCase();
    		for( i=0;i<s.length();i++){
    		if(Character.isAlphabetic(s.charAt(i)))	st.append(s.charAt(i));
    			
    		}
    		s=st.toString();
    		 
            len=s.length();
    		for(int c=0; c<s.length(); c++) {
    	    charNum = s.charAt(c);
    	    for(int z=0;z<26;z++){
    		if(charNum==(char)(65+z))
    		cnt[z]+=1;
    	    }
    		}
    	    for(int z=0;z<len;z++){
    	    	charNum = s.charAt(z);
    	    	if(charNum=='A'||charNum=='E'||(charNum)=='I'||charNum=='O'||charNum=='U'||charNum=='İ')vow++;
    	    	
    
    	    
    	    
    	    
    	    }
    
    	    for(int c=0; c<s.length(); c++) {
    		    
    	for(i=0;i<26;i++)System.out.println(lettersAlph[i] +" = " + cnt[i]);
    	    }
    	System.out.println("There are    "+ vow +"   vowels");
    	System.out.println("There are    "+ (s.length()-vow) +"   consonants");
    	System.out.println(s);
    	int j=0,l=0;
    	for(int k=0;k<s.length();k++){
    		if(isVowel(s.charAt(k)))j++;
    		}
       
    
    	for(int k=0;k<s.length();k++){
    		if(isConsonant(s.charAt(k)))l++;
    		}
       
    	System.out.println("There are "+j+"  Vowels");
    	
    	System.out.println("There are "+l+"  Consonants");
    boolean boo=true;	
    try {
    	for(int z=0;z<s.length();z++){
    		if(isConsonant(s.charAt(z))&& isConsonant(s.charAt(z+1))) {
    			System.out.println(s+"    is   "+" NOT alternating");boo=false;break;
    	
    		}
    		
    		if(!isConsonant(s.charAt(z))&& !isConsonant(s.charAt(z+1))) {
    			System.out.println(s+"    is   "+" NOT alternating");boo=false;break;
    	
    		}
    	}
    } catch (Exception e) {
    	 
    }
    if(boo)System.out.println(s+"    is   "+"   alternating");
    
     
    	}
    static boolean isVowel(char charNum){
    	if(charNum=='A'||charNum=='E'||charNum=='I'||charNum=='O'||charNum=='U'||charNum=='İ')return true;
    	return false;
    }
    
    static boolean isConsonant(char charNum){
    	boolean bool=!isVowel(charNum);
    	if(bool)return true;
    	return false;
    }
    
    }

    Comments on this post

    • NormR disagrees : How does giving code help OP learn to solve problems
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    39
    Rep Power
    2
    Thats what I need to figure out
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    39
    Rep Power
    2
    Originally Posted by NormR
    That's the part that needs to be described. What are the steps for the "somehow" part?
    Thats what I need to figure out
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    39
    Rep Power
    2
    Originally Posted by burakaltr
    Code:
    import java.util.Scanner;
    
    import javax.swing.JOptionPane;
    
    public class ScanningText {
     	public static void main(String[] args) {
    		  String s;  StringBuilder st=new StringBuilder("");  int vow=0,len =0;  int charNum,i;
    
    		
    		
    		int[] letters = new int[26];
            char lettersAlph[]=new char[26];
            int cnt[]=new int[26];
            for(i=0;i<26;i++)
            {
            lettersAlph[i]= (char) (65+i);	
            cnt[i]=0;	
             }
            	   	
             
             
            s=JOptionPane.showInputDialog("Enter a string");
    		 s=s.toUpperCase();
    		for( i=0;i<s.length();i++){
    		if(Character.isAlphabetic(s.charAt(i)))	st.append(s.charAt(i));
    			
    		}
    		s=st.toString();
    		 
            len=s.length();
    		for(int c=0; c<s.length(); c++) {
    	    charNum = s.charAt(c);
    	    for(int z=0;z<26;z++){
    		if(charNum==(char)(65+z))
    		cnt[z]+=1;
    	    }
    		}
    	    for(int z=0;z<len;z++){
    	    	charNum = s.charAt(z);
    	    	if(charNum=='A'||charNum=='E'||(charNum)=='I'||charNum=='O'||charNum=='U'||charNum=='İ')vow++;
    	    	
    
    	    
    	    
    	    
    	    }
    
    	    for(int c=0; c<s.length(); c++) {
    		    
    	for(i=0;i<26;i++)System.out.println(lettersAlph[i] +" = " + cnt[i]);
    	    }
    	System.out.println("There are    "+ vow +"   vowels");
    	System.out.println("There are    "+ (s.length()-vow) +"   consonants");
    	System.out.println(s);
    	int j=0,l=0;
    	for(int k=0;k<s.length();k++){
    		if(isVowel(s.charAt(k)))j++;
    		}
       
    
    	for(int k=0;k<s.length();k++){
    		if(isConsonant(s.charAt(k)))l++;
    		}
       
    	System.out.println("There are "+j+"  Vowels");
    	
    	System.out.println("There are "+l+"  Consonants");
    boolean boo=true;	
    try {
    	for(int z=0;z<s.length();z++){
    		if(isConsonant(s.charAt(z))&& isConsonant(s.charAt(z+1))) {
    			System.out.println(s+"    is   "+" NOT alternating");boo=false;break;
    	
    		}
    		
    		if(!isConsonant(s.charAt(z))&& !isConsonant(s.charAt(z+1))) {
    			System.out.println(s+"    is   "+" NOT alternating");boo=false;break;
    	
    		}
    	}
    } catch (Exception e) {
    	 
    }
    if(boo)System.out.println(s+"    is   "+"   alternating");
    
     
    	}
    static boolean isVowel(char charNum){
    	if(charNum=='A'||charNum=='E'||charNum=='I'||charNum=='O'||charNum=='U'||charNum=='İ')return true;
    	return false;
    }
    
    static boolean isConsonant(char charNum){
    	boolean bool=!isVowel(charNum);
    	if(bool)return true;
    	return false;
    }
    
    }
    I cant get it to work, it says there is something wrong with the isAlphabetic, and I dont want all that complicated stuff, I cant understand it, this program I am trying to make is simple, it is just a small part of a big project, its not meant to be like a 100 lines long full of code I cant understand
  28. #15
  29. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    What part are you having problems with?
    Do you know how to determine if a letter is a vowel or consonant?
Page 1 of 7 123 ... Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo