Page 1 of 2 12 LastLast
Results 1 to 20 of 29

Thread: Vowels

  1. #1
    hockey101 is offline Member
    Join Date
    Dec 2012
    Posts
    44
    Rep Power
    0

    Default Vowels

    Hello everyone...I am new.....I am having problems writing a and starting a project that will read a String from the keyboard, and count the number of each of the vowels. My program also needs to count other characters. There should be a separate variable as a counter for each. I need to use for loops and switch statements. Any ideas?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Vowels

    Let's start with your ideas -- what have you tried, and where exactly are you stuck? It would be best if you show us your current code, but have the posted code wrapped in code tags:

    [code]
    your posted code
    [/code]

    Also remember to break down any project into small steps and try to solve each small step one at a time so as not to get overwhelmed.

  3. #3
    hockey101 is offline Member
    Join Date
    Dec 2012
    Posts
    44
    Rep Power
    0

    Default Re: Vowels

    Alright here is what i got so far....
    Java Code:
    package vowels;
    
    
    import java.util.Scanner;
    public class Main
    {
        public static void main(String[] args) 
        {
            int aCount=0, eCount=0, iCount=0, oCount=0, uCount=0, other=0;
            String strCount;
    
            Scanner scan = new Scanner(System.in);
    
            System.out.println("Please Enter a string of characters: ");
            aCount = scan.nextInt();
            eCount = scan.nextInt();
            iCount = scan.nextInt();
            oCount = scan.nextInt();
            uCount = scan.nextInt();
            other = scan.nextInt();
            switch (aCount)
            {
                case 1: strCount = "a: " + aCount;
                break;
            }
            switch (eCount)
            {
                case 2: strCount = "e: " + eCount;
                break;
            }
            switch (iCount)
            {
               case 3: strCount = "i: " + iCount;
               break;
            }
            switch (oCount)
            {
                case 4: strCount = "o: " + oCount;
                break;
            }
            switch (uCount)
            {
                case 5: strCount = "u: " + uCount;
                break;
            }
        }
    
    }
    1st: I know this is not logically correct. I just started typing in random things to see what could fit together. I know the code is more complicated then this.

    2nd: I don't want to type in a string of characters and then it to print out a e i o u and other characters separately. I want the computer to tell me how many vowels are in that string of characters and how many other characters as well. How do I do that?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Vowels

    Don't code random things as this never works. Instead think logically through the problem and then try to use that logic in your code. The logic could start with something like, if you were given a sentence on a piece of paper, how would you manually go through the sentence and count the number of vowels.

  5. #5
    hockey101 is offline Member
    Join Date
    Dec 2012
    Posts
    44
    Rep Power
    0

    Default Re: Vowels

    That's my problem, I have no idea where to start. I don't know what half these things mean and Ive read the chapter like 2 times....nothing helps.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Vowels

    You will not be able to solve this until you understand Java a little better. Consider trying to study from a different tutorial as sometimes you can learn better from one source vs another.

    Please have a look here: The Really Big Index

  7. #7
    hockey101 is offline Member
    Join Date
    Dec 2012
    Posts
    44
    Rep Power
    0

    Default Re: Vowels

    Ok i have redone this program....here is what I got..
    Java Code:
    package vowels;
    
    
    import java.util.Scanner;
    
    public class Main
    {
        public static void main(String[] args) 
        {
            
            int aCount=0, eCount=0, iCount=0, oCount=0, uCount=0;
            
    
            Scanner scan = new Scanner (System.in);
            String strCount;
            
    
            System.out.println("Please Enter a string of characters: ");
            strCount = scan.nextLine();
    
            char vowels = 0;
    
    
    
            for (int count = 0; count < strCount.length(); count++)
            {
                vowels = strCount.charAt(count);
            switch (vowels)
            {
            
                case 'a':
                    aCount++;
                    break;
            
                case 'e':
                    eCount++;
                    break;
            
            
                case 'i':
                    iCount++;
                    break;
            
            
                case 'o':
                    oCount++;
                    break;
            
            
                case 'u':
                    uCount++;
                    break;
             
            }
    
                        System.out.println ("a: " + aCount);
                        System.out.println ("e: " + eCount);
                        System.out.println ("i: " + iCount);
                        System.out.println ("o: " + oCount);
                        System.out.println ("u: " + uCount);
                        System.out.println ("Constants = " + (strCount.length() - (aCount + eCount + iCount + oCount + uCount)));
            }
        }
    }
    But when I run that program it prints out this.....
    Please Enter a string of characters:
    Hello there! My name is Ryan.
    a: 0
    e: 0
    i: 0
    o: 0
    u: 0
    Constants = 29
    a: 0
    e: 1
    i: 0
    o: 0
    u: 0
    Constants = 28
    a: 0
    e: 1
    i: 0
    o: 0
    u: 0
    Constants = 28
    a: 0
    e: 1
    i: 0
    o: 0
    u: 0
    Constants = 28
    a: 0
    e: 1
    i: 0
    o: 1
    u: 0
    Constants = 27
    a: 0
    e: 1
    i: 0
    o: 1
    u: 0
    Constants = 27
    a: 0
    e: 1
    i: 0
    o: 1
    u: 0
    Constants = 27
    a: 0
    e: 1
    i: 0
    o: 1
    u: 0
    Constants = 27
    a: 0
    e: 2
    i: 0
    o: 1
    u: 0
    Constants = 26
    a: 0
    e: 2
    i: 0
    o: 1
    u: 0
    Constants = 26
    a: 0
    e: 3
    i: 0
    o: 1
    u: 0
    Constants = 25
    a: 0
    e: 3
    i: 0
    o: 1
    u: 0
    Constants = 25
    a: 0
    e: 3
    i: 0
    o: 1
    u: 0
    Constants = 25
    a: 0
    e: 3
    i: 0
    o: 1
    u: 0
    Constants = 25
    a: 0
    e: 3
    i: 0
    o: 1
    u: 0
    Constants = 25
    a: 0
    e: 3
    i: 0
    o: 1
    u: 0
    Constants = 25
    a: 0
    e: 3
    i: 0
    o: 1
    u: 0
    Constants = 25
    a: 1
    e: 3
    i: 0
    o: 1
    u: 0
    Constants = 24
    a: 1
    e: 3
    i: 0
    o: 1
    u: 0
    Constants = 24
    a: 1
    e: 4
    i: 0
    o: 1
    u: 0
    Constants = 23
    a: 1
    e: 4
    i: 0
    o: 1
    u: 0
    Constants = 23
    a: 1
    e: 4
    i: 1
    o: 1
    u: 0
    Constants = 22
    a: 1
    e: 4
    i: 1
    o: 1
    u: 0
    Constants = 22
    a: 1
    e: 4
    i: 1
    o: 1
    u: 0
    Constants = 22
    a: 1
    e: 4
    i: 1
    o: 1
    u: 0
    Constants = 22
    a: 1
    e: 4
    i: 1
    o: 1
    u: 0
    Constants = 22
    a: 2
    e: 4
    i: 1
    o: 1
    u: 0
    Constants = 21
    a: 2
    e: 4
    i: 1
    o: 1
    u: 0
    Constants = 21
    a: 2
    e: 4
    i: 1
    o: 1
    u: 0
    Constants = 21
    BUILD SUCCESSFUL (total time: 7 seconds)

    What is wrong with my program?

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Vowels

    Hey, that's starting to look pretty good!

    The main thing that I see wrong is the location of where you're outputting your results. Consider outputting the results outside of the for loop, in other word after the closing curly brace of the for loop. Note that you have not indented your switch block inside of the for loop and so it is hard to see where one block ends. This is why it is so critical to be precise and consistent with your indentation and code formatting.

    Good going so far!

  9. #9
    hockey101 is offline Member
    Join Date
    Dec 2012
    Posts
    44
    Rep Power
    0

    Default Re: Vowels

    Alright how about this.....
    Java Code:
    package vowels;
    
    
    import java.util.Scanner;
    
    public class Main
    {
        public static void main(String[] args) 
        {
            
            int aCount=0, eCount=0, iCount=0, oCount=0, uCount=0, count = 0;
            
    
            Scanner scan = new Scanner (System.in);
            String strCount;
            
    
            System.out.println("Please Enter a string of characters: ");
            strCount = scan.nextLine();
    
            char vowels = 0;
    
    
    
    
            for (count = 0; count < strCount.length(); count++)
    
                 
            {
            vowels = strCount.charAt(count);
            
                switch (vowels)
            {
            
                case 'a':
                    aCount++;
                    break;
            
                case 'e':
                    eCount++;
                    break;
            
            
                case 'i':
                    iCount++;
                    break;
            
            
                case 'o':
                    oCount++;
                    break;
            
            
                case 'u':
                    uCount++;
                    break;
             
            }
            
            }
                        System.out.println ("a: " + aCount);
                        System.out.println ("e: " + eCount);
                        System.out.println ("i: " + iCount);
                        System.out.println ("o: " + oCount);
                        System.out.println ("u: " + uCount);
                        System.out.println ("Constants = " + (strCount.length() - (aCount + eCount + iCount + oCount + uCount)));
        }
    }
    then it prints out this......

    Please Enter a string of characters:
    Hello there! My name is Ryan.
    a: 2
    e: 4
    i: 1
    o: 1
    u: 0
    Constants = 21
    BUILD SUCCESSFUL (total time: 8 seconds)

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Vowels

    You tell me.... how does the output look to you?

    Regarding the code formatting -- it still needs work. Remember, any block nested inside of another block needs to be indented from the containing block. Again your switch block is not. In fact your whole formatting looks a bit off kilter. Look at some of the better code examples on this forum to see how to do this correctly.

  11. #11
    hockey101 is offline Member
    Join Date
    Dec 2012
    Posts
    44
    Rep Power
    0

    Default Re: Vowels

    I guess it looks ok...but I dont think its counting !, spaces, periods, and any other character. Plus it won't count capital letters. After I get that step, i need to put a while loop in to ask the user if they want to enter another string by using y and for the program to keep reading in strings and counting their vowels and other characters. This is so complicated................

  12. #12
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Vowels

    in regards to counting capital letters couldnt you tell it to ignore case so A is equal to an a;

    so something like

    Java Code:
    String tennis="";
    while(tennis.equalsIgnoreCase (" "))
    {
    
    //then do switch stuff
    
    }
    sorry if im wrong, i based that off a program we created in school, it might not apply to yours

  13. #13
    hockey101 is offline Member
    Join Date
    Dec 2012
    Posts
    44
    Rep Power
    0

    Default Re: Vowels

    well the prob with that is that I still need that for loop in there. And my while loop will be for asking the user if they want to do another or not. I can try some of that though and ill see what results i get.

  14. #14
    hockey101 is offline Member
    Join Date
    Dec 2012
    Posts
    44
    Rep Power
    0

    Default Re: Vowels

    ya i have no idea where on earth I would put that...lol.....i tried putting it in but it completely messed up my program....im probably putting it in wrong somewhere

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Vowels

    Quote Originally Posted by hockey101 View Post
    I guess it looks ok...but I dont think its counting !, spaces, periods, and any other character.
    Do you want it to count these?

    Plus it won't count capital letters.
    Two easy ways to solve this: 1) convert the entire String to lower-case before analyzing it. String has a method that allows you to do this, or 2) change your case statements to recognize both upper and lower case letters:

    Java Code:
    case 'a': case 'A':
       // do something for a
       break;
    case 'b': case 'B':
       // do something for b
       break;

    After I get that step, i need to put a while loop in to ask the user if they want to enter another string by using y and for the program to keep reading in strings and counting their vowels and other characters. This is so complicated................
    Solve one step at a time. Once you get the main program logic working, then wrap all that needs to loop in a while loop.

  16. #16
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Vowels

    im not even sure thats correct! my program might be completely different to yours, just trying to help a bit :)

    you could still have the for loop, and the while loop you are aware of nesting loops arent you? *not to sound degrading

    but as i say this might not be a correct answer, you could just use an or to say a OR A then count?

  17. #17
    hockey101 is offline Member
    Join Date
    Dec 2012
    Posts
    44
    Rep Power
    0

    Default Re: Vowels

    [QUOTE=Fubarable;312198]



    Java Code:
    case 'a': case 'A':
       // do something for a
       break;
    case 'b': case 'B':
       // do something for b
       break;
    Hey!!! That worked!!!!


    Do you want it to count these?
    Yes I do want my program to count these.

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Vowels

    Quote Originally Posted by hockey101 View Post
    Yes I do want my program to count these.
    The length() method will count all chars in a String.

  19. #19
    hockey101 is offline Member
    Join Date
    Dec 2012
    Posts
    44
    Rep Power
    0

    Default Re: Vowels

    Quote Originally Posted by monkeyjr97 View Post
    im not even sure thats correct! my program might be completely different to yours, just trying to help a bit :)

    you could still have the for loop, and the while loop you are aware of nesting loops arent you? *not to sound degrading

    but as i say this might not be a correct answer, you could just use an or to say a OR A then count?
    hahaha yes i do but i hardly know anything about this....im in a class with smart kids who dont care about anyone but themselves....then there is kids who don't give a crap about this...and then there is me....taking notes like a madman....I want to learn..it's just so difficult. Plus when I take notes...the teacher writes on the board what he doesn't want to see. So im taking notes on the stuff he doesn't want. Like really?

  20. #20
    hockey101 is offline Member
    Join Date
    Dec 2012
    Posts
    44
    Rep Power
    0

    Default Re: Vowels

    Quote Originally Posted by Fubarable View Post
    The length() method will count all chars in a String.
    I thought I already had the length() method in my program?

Page 1 of 2 12 LastLast

Similar Threads

  1. Counting Vowels, getting an error
    By gabrielpr12 in forum New To Java
    Replies: 8
    Last Post: 01-07-2012, 08:50 PM
  2. Replies: 2
    Last Post: 11-08-2010, 09:01 AM
  3. counting the vowels in a sentence
    By huntrguy102 in forum New To Java
    Replies: 3
    Last Post: 11-08-2010, 07:31 AM
  4. Replacing Vowels in a word.
    By mklprasad in forum Advanced Java
    Replies: 1
    Last Post: 10-05-2009, 01:31 PM
  5. Counting Vowels and Constonants
    By MattN in forum New To Java
    Replies: 3
    Last Post: 11-20-2007, 06:45 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
  •