Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: Counting Vowels and Consonants in a String.

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Counting Vowels and Consonants in a String.

    Hello everyone, I am so close to achieving my goal with this program. I just can't seam to figure out where the for loop goes wrong and "mis-counts" the amount of consonants and vowels. Also, in the else...if statement if anyone has advise to make it so it excludes all characters (like !, -, ...etc) besides vowels that would help so much! Part of the consonant problem I believe is it is counting spaces and other characters maybe?
    Thanks so much in advance for any help!

    import java.lang.String;
     
    public class StringProcessor 
    {
        String string;
        int vowels;
        int consonants;
     
        public void Count()
        {
            vowels = 0;
            consonants = 0;
            for(int i = 0; i < string.length(); i++)
            {
                char c = string.charAt(i);
                if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
                    vowels++;
                else
                    if(c != 'a' || c != 'e' || c != 'i' || c != 'o' || c != 'u' || c != ' ')
                    consonants++;               
            }
        }
     
        public void display()
        {
            System.out.println(string + " has " + vowels + " vowels and " 
                    + consonants + " consonants.");
        }
     
        public StringProcessor(String aString)
        {
            string = aString;
        }
     
        public void setString(String stString)
        {
            string = stString;
        }
    }
     
    public class TestProcessor 
    {
        public static void main(String[] args)
        {
            StringProcessor processor = new StringProcessor("Java Rules");
            processor.Count();
            processor.display();
     
            processor.setString("I like on-line classes");
            processor.Count();
            processor.display();
     
            processor.setString("Spring break was AWESOME!");
            processor.Count();
            processor.display();
        }
    }

    The output is supposed to look like this:

    "Java Rules" has 4 vowels and 5 consonants
    "I like on-line classes" has 8 vowels and 10 consonants
    "Spring break was AWESOME!" has 8 vowels and 13 consonants

    And I get this:

    Java Rules has 4 vowels and 6 consonants.
    I like on-line classes has 7 vowels and 15 consonants.
    Spring break was AWESOME! has 4 vowels and 21 consonants.


    Thanks again!

  2. The Following User Says Thank You to Andyandhisboard For This Useful Post:

    JavaWiz (January 8th, 2013)


  3. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Counting Vowels and Consonants in a String.

    Go through your for loop character by character, tracing through what's going to happen in each case. Think about it. What happens when c == ' '?

    Also, wouldn't it just be easier to have a List (or even a String) that contains all of the vowels, and use that to check?

    Also also, you might want to check out the methods in the Character class. The API is your friend.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Counting Vowels and Consonants in a String.

    Thanks man, now do you mind me asking, are you saying in my else...if for consonants I should set c == 'b' c == 'c' and so on and so forth for EVERY consonant? Also, how would I go about doing:

    "Also, wouldn't it just be easier to have a List (or even a String) that contains all of the vowels, and use that to check?"

    You wouldn't mind just providing an example of how you can use a "list" to check if it's a vowel or consonant?

    Thanks so much!

  5. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Counting Vowels and Consonants in a String.

    Quote Originally Posted by Andyandhisboard View Post
    Thanks man, now do you mind me asking, are you saying in my else...if for consonants I should set c == 'b' c == 'c' and so on and so forth for EVERY consonant?
    Not at all. I'm saying you should run through that code, especially at that line, to see what's going on. Pay close attention to what happens for the space character.

    Quote Originally Posted by Andyandhisboard View Post
    You wouldn't mind just providing an example of how you can use a "list" to check if it's a vowel or consonant?
    Here's some pseudo-code that checks to see if an animal is in a zoo:

    List<String> animals = new ArrayList<String>();
    animals.add("Tiger");
    animals.add("Penguin");
     
    System.out.println("Is a Tiger in the zoo: " + animals.contains("Tiger")); //true
    System.out.println("Is a Lion in the zoo: " + animals.contains("Lion")); //false
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #5
    Junior Member
    Join Date
    Mar 2011
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Counting Vowels and Consonants in a String.

    Thanks again

    I'm afraid I don't understand what happens when I compare c == " ". My guess would be maybe the space would represent any character that is a part of the string?

    Also, so if I were to make a list like the following? How would I compare it to the string that were testing from the getString method?

    List<String> vowel = new ArrayList<String>();
    vowel.add('a');
    vowel.add('e');
    vowel.add('i');
    vowel.add('o');
    vowel.add('u');

    And thanks so much for your help, I really do appreciate it!!!

  7. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Counting Vowels and Consonants in a String.

    Quote Originally Posted by Andyandhisboard View Post
    I'm afraid I don't understand what happens when I compare c == " ". My guess would be maybe the space would represent any character that is a part of the string?
    Nope. And there is no need for guessing- simply trace through your program, by hand or with a debugger, and see what it's doing on that line. Hint- what happens when the space character (or any non-vowel) is tested against the first condition (being not equal to 'a')?

    Quote Originally Posted by Andyandhisboard View Post
    Also, so if I were to make a list like the following? How would I compare it to the string that were testing from the getString method?
    Sort of, but you might want to use a List of Characters instead of Strings. Up to you. And you'd test it the same way, by each character. Only instead of comparing the characters in the test String separately to each vowel, you'd simply test whether it was in the List. It just tidies up your if statements.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #7
    Junior Member
    Join Date
    Mar 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Counting Vowels and Consonants in a String.

    Wouldn't it be i <= string.length() in your for loop parameters, as you're not including the last character in your string (if I'm not mistaken)

  9. #8
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Counting Vowels and Consonants in a String.

    No, you are mistaken. Strings, like arrays, are zero based. So for a String with a length of 5, characters exist at indicies of 0-4. Length, that is 5, would be invalid.

  10. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Counting Vowels and Consonants in a String.

    Hey OP, did you get this figured out? You were pretty close yesterday, and I'm just wondering if you ever figured out what I was getting at.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #10
    Junior Member
    Join Date
    Feb 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Counting Vowels and Consonants in a String.

    first of all it s better if you convert all letters to uppercase or to lowcase. This fixes one logic problem. After that erase the second if and replace with something like if(c != ' '). Make conditions for every non-vowel character. I hope this help you

  12. #11
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Counting Vowels and Consonants in a String.

    Gotta love spoonfeeding...
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  13. #12
    Junior Member
    Join Date
    Mar 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Counting Vowels and Consonants in a String.

    Quote Originally Posted by Junky View Post
    No, you are mistaken. Strings, like arrays, are zero based. So for a String with a length of 5, characters exist at indicies of 0-4. Length, that is 5, would be invalid.
    Ah yes, thank you for alerting me about that.

  14. #13
    Junior Member
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Counting Vowels and Consonants in a String.

    the miscount is due to the for loop you need a <= instead of a < otherwise it will stop counting one chracter before the end. I found your program/applet ver useful. Thank you.

  15. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Counting Vowels and Consonants in a String.

    @JavaWiz You're a little late posting on a 22 month old thread.
    the for loop you need a <= instead
    That usually causes the index to go past the end of the array. The index for the last element in the array is array length-1
    If you're not sure, write a small simple program that scans a String and prints out each character in the String.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #15
    Junior Member
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Counting Vowels and Consonants in a String.

    Srry fail i just relized why it had to be the other way ... Fail

Similar Threads

  1. [SOLVED] Program to find how many letters start with vowels
    By Lokesh in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 14th, 2011, 05:58 AM
  2. Im so confused! Counting and Summing
    By captiancd89 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 28th, 2010, 06:49 PM
  3. Counting cells
    By Shyamz1 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 28th, 2010, 05:04 PM
  4. Question: counting
    By miss confused in forum Java Theory & Questions
    Replies: 2
    Last Post: July 30th, 2010, 05:38 PM
  5. Counting Vowels and Prepositions in a text file
    By maybach230 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 27th, 2010, 07:36 PM