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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 42

Thread: Java for loops to count vowels/consonants usinf the logic of the main

  1. #1
    Member
    Join Date
    Feb 2012
    Posts
    35
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Default Java for loops to count vowels/consonants usinf the logic of the main

    When I enter 1 vowel the ans. is 1 vowel and 2 consonants. When I enter 1 vowel and 1 consonant the ans. is 1 v & 2 cons. When I enter 1 consonant the ans. is 0 v & 1 cons. If I enter the sentence: Welcome to Foothill. the ans. is 7 vowels & 8 consonants. The vowels are correct, but the consonants should be 10. I have tried several ways (for loop w/ if else & else and putting the control stat. within the braces, I have tried Strings & arrays but my knowledge is lacking, for ex. on the array used to count the chars my ans would give me a number value. I have reread my text book on loops & control statements several times however I must be missing the key concept to take care of my problem.
    import java.util.Scanner;
    public class NewFoothill
    {
       public static void main(String [] args) throws Exception
       {
           Scanner input = new Scanner (System.in);
           int count = 0;
           System.out.println(" Enter the String. ");
           String s1 = input.nextLine();
     
           s1 = s1.toUpperCase();
           System.out.println("======RESULT======" + s1);
           s1 = s1.toLowerCase();
           System.out.println("======RESULT======" + s1);
           System.out.println(" String s1 ");
     
               for (int i = 0; i < s1.length(); i++)
               {
                    char c = s1.charAt(i);
     
                     if ( c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' )
                     {
                          count++;
                     }
                }
                System.out.println(" There are " + " " + count + " " + " vowels. ");
     
                for (int i = 0; i < s1.length(); i++)
                {
                     char c = s1.charAt(i);
     
                     if  ( c != 'a' || c != 'e' || c != 'i' || c != 'o' || c != 'u' )
                     {
                          count++;
                          break;
                      }
                 }
                 System.out.println(" There are ' + " " + count + " " + " consonants. ");
        }
    }
    Last edited by helloworld922; April 7th, 2012 at 11:27 AM. Reason: please use [code] tags


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java for loops to count vowels/consonants usinf the logic of the main

    One logic problem I see is this:
    if ( c != 'a' || c != 'e' || c != 'i' || c != 'o' || c != 'u' )

    If any one of those conditions is true (OR connected expression) then the whole condition will be true. If c is 'e' then it is not equal to 'a'

    When do you want the condition to be true?

    Another way to determine if a letter is in a group of letters would be to make a String of all the letters in the group and use the index of method to see if the selected letter is in the String.


    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    willie lee (April 16th, 2012)

  4. #3
    Member
    Join Date
    Feb 2012
    Posts
    35
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Default Re: Java for loops to count vowels/consonants usinf the logic of the main

    Norm thanks for your reply. I separated the if statements in each for loop. When I remove the loop that counts consonants my loop that counts vowels works fine and I get the same result when I remove the loop that counts vowels, my loop that counts consonants works fine. However, when I put them together there is a problem. For example when I enter a vowel that is counted correctly, but the loop jumps down and adds five consonants. I believe it counts the five vowels that I have asked the consonant loop to not count. How can I end this problem and get the two loops to work together and give me the correct output?

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java for loops to count vowels/consonants usinf the logic of the main

    Post your current code.
    Can you describe in detail in pseudo how the program should work to solve the problem? Write a list of the steps the program should do.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    willie lee (April 16th, 2012)

  7. #5
    Member
    Join Date
    Feb 2012
    Posts
    35
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Default Re: Java for loops to count vowels/consonants usinf the logic of the main

    Thanks Norm for your reply. In each loop I separated each if statement. The loop that counts vowels works fine alone. I get the same good outcome when the loop that counts the consonants is alone without the other loop above it. If I enter 1 vowel, it is counted correctly, but the program goes down to the consonant loop and counts five consonants ( answer: There are 1 vowels. There are 5 consonants.). I do not understand why the for loop that counts consonants counts the five vowels that I asked that loop to not count. Since your last post to me, I have gone back to my textbooks, but I have not found an answer that works. I saw in some code that was used to get a password that boolean validated and not validated was used. Would something like that work? I am going crazy over this. It took me a very long time to ask for help and I realy need it now.

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java for loops to count vowels/consonants usinf the logic of the main

    Did you miss my last post? Here it is again:
    Post your current code.
    Can you describe in detail in pseudo code how the program should work to solve the problem? Write a list of the steps the program should do.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #7
    Member
    Join Date
    Feb 2012
    Posts
    35
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Default Re: Java for loops to count vowels/consonants usinf the logic of the main

    Hi Norm, I got your reply and I did what you asked of me, but before I sent my new quick reply I went up and hit the thanks button before I sent my new reply with the code then I scrolled down to my new quick reply and it was gone. Did you get it? If not can I get my quick reply back so I can send it?

  10. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java for loops to count vowels/consonants usinf the logic of the main

    I guess it's gone. You'll have to try again.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #9
    Member
    Join Date
    Feb 2012
    Posts
    35
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Default Re: Java for loops to count vowels/consonants usinf the logic of the main

    Norm, it is 9:33PM here and my wife is out of town and my 5 year old wants me to feed him so I must sign off. I will try to contact you tomorrow. Wille Lee

  12. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java for loops to count vowels/consonants usinf the logic of the main

    no problems. Whenever you get to it.
    If you don't understand my answer, don't ignore it, ask a question.

  13. The Following User Says Thank You to Norm For This Useful Post:

    willie lee (April 24th, 2012)

  14. #11
    Member
    Join Date
    Feb 2012
    Posts
    35
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Default Re: Java for loops to count vowels/consonants usinf the logic of the main

    Here is my current code Norm.
    import java.util.Scanner;
    public class NewFoothill
    {
       public static void main(String[] args) throws Exception
       {
           Scanner input = new Scanner(System.in);
     
           int count = 0;
     
           System.out.println(" Enter the String ");
           String s1 = input.nextLine();
     
           s1 = s1.toUpperCase();
           System.out.println("=====RESULT=====" + s1);
           s1 = s1.toLowerCase();
           System.out.println("=====RESULT=====" + s1);
           System.out.println(" String s1 ");
     
              for (int i = 0; i < s1.length(); i++)
              {
                 char c = s1.charAt(i);
     
                 if ( c == 'a' )
                 {
                    count++;
                 }
                 if ( c == 'e' )
                 {
                    count++;
                 }
                 if ( c == 'i' )
                 {
                    count++;
                 }
                 if ( c == 'o' )
                 {
                    count++;
                 }
                 if ( c == 'u' )
                 {
                    count++;
                    continue;
                  }
               }
               System.out.println(" There are " + " " + count + " " + " vowels. ");
     
               for ( int i = 0; i < s1.length(); i++)
               {
                  char c = s1.charAt(i);
     
                  if ( c == 'c' )
                  {
                     count++;
                     continue;
                   }
                   if ( c == 'f' )
                   {
                      count++;
                      continue;
                   }
                   if ( c == 'w' )
                   {
                      count++;
                      continue;
                   }
                   if ( c == 'l' )
                   {
                      count++;
                      continue;
                    }
                    if ( c == 'm')
                    {
                       count++;
                       continue;
                    }
                    if ( c == 't' )
                    {
                       count++;
                       continue;
                    }
                    if ( c == 'h' )
                    {
                       count++;
                       continue;
                    }
                    if ( c != 'a' )
                    {
                       count++;
                       break;
                     }
                     if ( c != 'e' )
                     {
                        count++;
                        break;
                      }
                      if ( c != 'i' )
                      {
                         count++;
                         break;
                       }
                       if ( != 'o' )
                       {
                          count++;
                          break;
                        }
                        if ( c != 'u' )
                        {
                           count++;
                           break;
                         }
                      }
                      System.out.println(" There are " + " " + count + " " + " consonants. ");
        }
    }

    The program should take the String input and send it to upper case & then to lower case. Next the 1st for loop following the String s1 should count all of the vowels in String s1. The following for loop should then count all of the consonants. I have tested each for loop independently & they work correctly. However, when the consonant loop follows the vowel loop the consonant loop does not work correctly. I took the sentence "Welcome to Foothill." to use as my test String & only entered the letters within that sentence as code in the program. In the 1st loop I stated that c equals each of the vowels separately. In the 2nd loop that counts the consonants I have c equaling the consonants that are in the above noted sentence & c not equaling the vowels so I wanted this loop to count only the consonants and not the vowels. However when I test the program I get these results: a = 1 vowel & 2 consonants. az = 1 v. & 2 c. z = 1 c. Welcome = 3 v. & 5c. to = 1 v. & 3 c. Foothill = 3 v. & 5 c. (which is correct) Welcome to Foothill. = 7 v. & 9 c. I have changed and reworked these for loops millions of times, but to no avail. I have reread my text books several times and I am still missing the point, that is why I turned to you for assistance. I have come against a brick wall.
    Last edited by willie lee; April 24th, 2012 at 10:11 AM.

  15. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java for loops to count vowels/consonants usinf the logic of the main

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting


    Does your version of the code compile without errors? The posted code has errors.
    Last edited by Norm; April 20th, 2012 at 10:12 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  16. The Following User Says Thank You to Norm For This Useful Post:

    willie lee (April 24th, 2012)

  17. #13
    Member
    Join Date
    Feb 2012
    Posts
    35
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Default Re: Java for loops to count vowels/consonants usinf the logic of the main

    Hi Norm, I posted all the information you asked for a 2nd time in the quick reply section and it looks like I posted my code again in the wrong place. This time I am trying the "Reply to Thread" section. This will be the 3rd attempt. If this does not work, please offer suggestions on how I can send the information and code to you.

    My goal, besides getting the code to you, is to have two for loops enter the sentence in String s1 and pull out the vowels and count them 1st and then I want the program to go down to the next for loop and do the same with the consonants. In order to achieve this goal I have used if statements which will be either true or false. In the for loop dealing with the vowels I have made a separate if statement for each vowel. I then tested the loop separately and it worked perfectly. Next, I set up the for loop to count the consonants. First, to keep the program shorter I choose the following sentence: Welcome to Foothill. I created separate if statements to count each consonant and I created separate if statements not count the vowels. I then tested my for loop used to count only the consonants separately and it worked correctly. My logic was to have if statements in the vowel loop that would be true to count only vowels and in the consonant loop I set up separate if statements to create true statements to count consonants and if statements to not count the vowels. At any rate, I then put both loops into the program. The 1st for loop counts the vowels and the 2nd one counts the consonants. Well, the result is as follows: enter the word Welcome, result: 3 vowels, 5 consonants. Enter the word "to," result: 1 v., 3 c. Enter the word "Foothill," result: 3 v., 5 c. (which is correct) Enter the sentence: Welcome to Foothill. Result: 7 v, 9 c. Enter the vowel "a," result: 1 v., 2 c. Enter "az," result: 1 v., 2 c. Enter "z," result: 0 v., 1 c. I do not understand why the 1st for loop that counts vowels will add consonants when I have not entered any (ex. "a," result: 1 v., 2 c.). So the big question is: Why does the for loop that should only count vowels add in consonants where none have been entered or add extra consonants when some have been entered. That is my problem. Here is my code:
    import java.util.Scanner;
    public class NewFoothill
    {
       public static void main(String[] args) throws Exception
    {
       Scanner input = new Scanner(System.in);
     
       int count = 0;
     
       System.out.println(" Enter the String ");
       String s1 = input.nextLine();
     
       s1 = s1.toUpperCase();
       System.out.println("=====RESULT=====" + s1);
       s1 = s1toLowerCase();
       System.our.println("=====RESULT=====" + s1);
       System.out.println(" String s1 ");
     
          for (int i = 0; i < s1.length(); i++)
          {
             char c = s1.charAt(i);
     
             if (c == 'a')
             {
                count++;
             }
             if (c == 'e')
             {
                count++;
             }
             if (c == 'i')
             {
                count++;
             }
             if (c == 'o')
             {
                count++;
             }
             if (c == 'u')
             {
                count++;
                continue;
              }
           }
           System.out.println(" There are " + " " + count + " " + " vowels. ");
     
           for (int i =0; i < s1.length(); i++)
           {
              char c = s1.charAt(i);
     
              if (c == 'c')
              {
                 count++;
                 continue;
              }
              if (c == 'f')
              {
                 count++;
                 continue;
               }
               if (c == 'w')
               {
                  count++;
                  continue;
               }
               if (c == 'l')
               {
                  count++;
                  continue;
               }
               if (c == 'm')
               {
                  count++;
                  continue;
               }
               if (c == 't')
               {
                  count++;
                  continue;
               }
               if (c == 'h')
               {
                  count++;
                  continue;
               }
               if (c != 'a')
               {
                  count++;
                  break;
               }
               if (c != 'e')
               {
                  count++;
                  break;
               }
               if (c != 'i')
               {
                  count++;
                  break;
               }
               if (c != 'o')
               {
                  count++;
                  break;
               }
               if (c != 'u')
               {
                  count++;
                  break;
               }
            }
            System.out.println(" There are " + " " +  count  + " " + " consonants. ");
         }
    }
    Last edited by willie lee; April 24th, 2012 at 09:48 AM.

  18. #14
    Member
    Join Date
    Feb 2012
    Posts
    35
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Default Re: Java for loops to count vowels/consonants usinf the logic of the main

    My code which I entered on 24/04/12 , 2351 hours (Thai time) compiles w/o errors. I use the Eclipse Europa. I edited my code so I hope it is now highlighted correctly and I corrected a miss spelling on String. Thanks for your help Norm.

  19. #15
    Member
    Join Date
    Feb 2012
    Posts
    35
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Default Re: Java for loops to count vowels/consonants usinf the logic of the main

    Yes, this code compiles w/o errors.

  20. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java for loops to count vowels/consonants usinf the logic of the main

    Please post the output from the program and add comments to it describing what is wrong and show what the output should be.

    Your long paragraphs hide that output if it is inside of them.

    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    If you don't understand my answer, don't ignore it, ask a question.

  21. The Following User Says Thank You to Norm For This Useful Post:

    willie lee (April 27th, 2012)

  22. #17
    Member
    Join Date
    Feb 2012
    Posts
    35
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Default Re: Java for loops to count vowels/consonants usinf the logic of the main

    Norm, did you receive the contents from my command prompt window along with my comments?

  23. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java for loops to count vowels/consonants usinf the logic of the main

    I don't see it. Where did you post it?
    If you don't understand my answer, don't ignore it, ask a question.

  24. The Following User Says Thank You to Norm For This Useful Post:

    willie lee (April 30th, 2012)

  25. #19
    Member
    Join Date
    Feb 2012
    Posts
    35
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Default Re: Java for loops to count vowels/consonants usinf the logic of the main

    Norm, I posted it in the quick reply box. It looks like I will have to do it again. I am unable to paste to this site because I cannot get my script blocker to allow it. I will copy the command prompt here and highlight it.
    Enter the String
    O
    =====RESULT=====O
    =====RESULT=====o
       String s1
       There are 1 vowels.
       There are 2 consonants.
    The vowel count is correct and the consonant count is not. It should be: There are 0 consonants.
    Enter the String
    W
    =====RESULT=====W
    =====RESULT=====w
       String s1
       There are 0 vowels.
       There are 1 consonants.
    The output is 100% correct.
    Enter the String
    Welcome!
    =====RESULT=====WELCOME!
    =====RESULT=====welcome!
       String s1
       There are 3 vowels.
       There are 5 consonants.
    The vowel count is correct. The consonant count is not. The count should be: There are 4 consonants.
    Enter the String
    OW
    =====RESULT=====OW
    =====RESULT=====ow
       String s1
       There are 1 vowels.
       There are 2 consonants.
    The vowel count is correct. The consonant count is not. The consonant count should be: There are 1 consonants.
    Enter the String
    Foothill
    =====RESULT=====FOOTHILL
    =====RESULT=====foothill
       String s1
       There are 3 vowels.
       There are 5 consonants.
    The answer is 100% correct.
    Enter the String
    Welcome to Foothill.
    =====RESULT=====WELCOME TO FOOTHILL.
    =====RESULT=====welcome to foothill.
       String s1
       There are 7 vowels.
       There are 9 consonants.
    The vowel count is correct. The consonant count is not. The consonant count should be: There are 10 consonants.
    Last edited by willie lee; April 30th, 2012 at 04:23 AM.

  26. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java for loops to count vowels/consonants usinf the logic of the main

    You should play computer with your program for one of the Strings that is not counted correctly. Take a piece of paper and Manually do what the code is supposed to do step by step to see where the mistake in your logic is.
    If you don't understand my answer, don't ignore it, ask a question.

  27. The Following User Says Thank You to Norm For This Useful Post:

    willie lee (May 1st, 2012)

  28. #21
    Junior Member
    Join Date
    May 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java for loops to count vowels/consonants usinf the logic of the main

    ....edited by moderator



    Try the above code u will get correct output

    Try commented one also..................
    Last edited by copeg; May 1st, 2012 at 01:50 PM.

  29. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java for loops to count vowels/consonants usinf the logic of the main

    How about helping the OP understand what his code is doing instead of spoonfeeding code with no comments or description of what it does.
    Read this:
    http://www.javaprogrammingforums.com...n-feeding.html
    If you don't understand my answer, don't ignore it, ask a question.

  30. The Following User Says Thank You to Norm For This Useful Post:

    copeg (May 1st, 2012)

  31. #23
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Java for loops to count vowels/consonants usinf the logic of the main

    @divi0520, please read the forum rules and the link in Norms post. I have edited your post

  32. #24
    Member
    Join Date
    Feb 2012
    Posts
    35
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Default Re: Java for loops to count vowels/consonants usinf the logic of the main

    Norm, I think the problem with my code resides within the control variable of my for loop that counts the vowels. The loop vowel count is fine, but it jumps down to the loop that counts consonants and adds a couple of them to the output. For example, if I enter 'a' my output comes back that I have 1 vowel and 2 consonants. I never entered any consonants. Furthermore, to make my consonant loop more precise I entered an if statement that states: if (c != 'a'). What do you think?

  33. #25
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java for loops to count vowels/consonants usinf the logic of the main

    Try debugging the code by Adding some println statements to the code to print out the values of the variables like the one that holds the current value of the count. The print out will help you see where the problem is.
    Print out the value every time the variable's value is changed.
    If you don't understand my answer, don't ignore it, ask a question.

  34. The Following User Says Thank You to Norm For This Useful Post:

    willie lee (May 18th, 2012)

Page 1 of 2 12 LastLast

Similar Threads

  1. Counting Vowels and Consonants in a String.
    By Andyandhisboard in forum What's Wrong With My Code?
    Replies: 14
    Last Post: January 8th, 2013, 09:59 PM
  2. Java Dos Logic Test count all non increasing number
    By Jhovarie in forum Object Oriented Programming
    Replies: 3
    Last Post: January 13th, 2011, 03:28 PM
  3. Replies: 6
    Last Post: November 12th, 2010, 04:40 AM
  4. Replies: 2
    Last Post: March 26th, 2010, 11:22 AM
  5. Creating A Count Matrix In Java
    By statsman5 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: July 14th, 2009, 04:40 PM