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 2 of 2 FirstFirst 12
Results 26 to 42 of 42

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

  1. #26
    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 entered a println inside my for loop for vowels just after the if statement about 'a'. I ended the loop just after 'a' and put in the println. A sea of red appeared but I continued with the launch. A notice appeared saying that, " A handler conflict occurred. This may disable some commands." All is not well with my debugger now. In the past, I have not used the debugger with logic problems.

    I have reread my textbook a 100 time concerning if statements. They are true or false. If I enter one vowel how can this machine state that I have entered one vowel and two consonants? In the consomnant loop I made if statements stating that the vowels were not to be counted. How can the vowel loop create two consonants out of thin air? Yes I know that I am missing something even though I have reread my texts concerning for loops and if statements. I will not give up and I will figure it out somehow and you will be the first to know. Thanks for your assistance.

  2. #27
    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

    What is printed out by the printlns you have added?

    If you get compiler errors, please copy and paste here the full text of the error messages.

    Instead of having a bunch of independent if statements, you should merge them by using the && operator:
    if(cond1 && cond2 && cond3) {...}

    or the || operator:
    if (cond1 || cond2 || cond3) { ...}

    One problem you may have with independent if statements is that more than one can be true.
    Last edited by Norm; May 18th, 2012 at 08:52 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #28
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

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

    I think you might have a copy-and-paste mistake. Are you aware that you are checking for vowels in your check for consonants? Furthermore, are you aware the you do not reset your count variable to 0 after you finish counting the vowels?

    Those two things combined would account for your program giving you the result: 1 vowel 2 consonants for the input of "a".

    This is not an issue your debugger will pick up, it is a logic mistake that you made while coding your if statements.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  4. The Following User Says Thank You to aussiemcgr For This Useful Post:

    willie lee (May 22nd, 2012)

  5. #29
    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

    aussiemcgr, I have changed my program as follows:
    [code = java]
    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++;
    continue;
    }
    }
    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++;
    continue;
    }

    if ( c == 'w' || c == 'l' || c == 'm' || c == 't' || c == 'f' || c == 'h')
    {
    count++;
    }
    }
    System.out.println(" There are " + " " + count + " " + " consonants. ");
    }
    }
    [/code]

    You noted that I should not reset my count variable to 0 after I finish counting vowels. I believe you are refering to the control variable of my for loop that controls the count of consonants. However, I am not sure if you are talking about the "initial action", loop-continuation-condition" or the "action-after-each-iteration". If you are talking about the "initial action" my text says that it can be a list of zero or more comma-separated variable declaration statements or assignment expressions. That is where I get lost. I tried leaving the initial action out but that did not work. I do not know the correct way to change it. Should I put a number there? If so what number? Right now it is
     (int i = 0;
    I have felt that my problem was in the control variable but I do not know how to correct it. I really do need your knowledge concerning this problem. To keep this test short I did not enter all the 21 consonants. I only entered the consonants within the test sentence: Welcome to Foothill. However I did enter all of the vowels into the loops. Again, let me thank you for your kind assistance. I have tried to figure this problem out on my own with the help of my text books, but I know I am missing something.

  6. #30
    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

    if ( c != 'a' || c !='e' || c != 'i' || c != 'o' || c != 'u')

    With OR (||) tests only one condition needs to be true for the if to be true.
    c = 'e' means c != 'a'
    In other words, that if will always be true.
    If you don't understand my answer, don't ignore it, ask a question.

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

    willie lee (May 23rd, 2012)

  8. #31
    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 changed my if statement in the for loop concerning consonants to read as follows:
    if (( c != 'a' ) && ( c != 'e' ) && ( c != 'i' ) && ( c != 'o' ) && ( c != 'u )
    {
       count++;
       continue;
    }

    console output
    Enter the String
    A
    =====RESULT=====A
    =====RESULT=====a
       String s1
       There are 1 vowels.
       There are 1 consonants.

    console output
    Enter the String
    Welcome to Foothill.
    =====RESULT=====WELCOME TO FOOTHILL.
    =====RESULT=====welcome to foothill.
       String s1
       There are 7 vowels.
       There are 20 consonants.

    Norm, progress has been made. In the past when I entered a vowel the output was always: 1 vowel and 2 consonants. Now I get 1 vowel and 1 consonant. The correct answer should be 1 vowel and 0 consonants.

    aussiemcgr in a post noted above that you do not reset the "count variable" to 0 after you finish counting the vowels. I do not understand how to do that correctly. Is that one of my problems? Thanks again for your continued help and support.

  9. #32
    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

    Why not have separate counters for each thing you are counting instead of using the same variable for more than one thing you are counting?
    If you don't understand my answer, don't ignore it, ask a question.

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

    willie lee (May 24th, 2012)

  11. #33
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

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

    You can do what Norm suggested (and use a different variable), or you can simply add the statement count = 0; after your System.out.println(" There are " + " " + count + " " + " vowels. "); statement. All that line does is sets the value of count back to 0 (which is the same as reseting it).
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  12. The Following User Says Thank You to aussiemcgr For This Useful Post:

    willie lee (May 24th, 2012)

  13. #34
    Junior Member
    Join Date
    Jul 2011
    Posts
    1
    Thanks
    0
    Thanked 1 Time in 1 Post

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

    You can try this
    if ( c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ) {
    // This checks to see if there is a vowel. For each letter checked in the loop if any of these are true than the current letter is a vowel.
    // Add the code you need every time you hit a vowel.
    }
    else {
    // If the if statement fails then the letter is obviously a constant. We dont need to check it. Just add your code to handle each constant
    // in the else statement. Every time the if statement fails it will jump right to this else statement. And if the if statement is true it will skip this
    // else statement.
    }

  14. The Following User Says Thank You to jcrosby10 For This Useful Post:

    willie lee (May 24th, 2012)

  15. #35
    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

    Aussiemcgr, I did what you suggested and all worked well concerning the proper counting of vowels and consonants. However, when I entered the target sentence (Welcome to Foothill.) the count of vowels was correct, but the count of consonants was not. My loop counted 13 consonants. There should be only 10. I checked the output for each word separately and the count was correct. Why does my target sentence not work? I will fiddle with my program again tomorrow. Thanks for your help. I am getting very close to making my program work correctly. Just a moment ago, I read another post and they mentioned that my program may be counting the white space, etc. concerning my loop that counts consonants so I got rid of the spaces and full stop in my target sentence (WelcometoFoothill) and the program worked correctly for the consonants. My vowel loop has always worked , but it seems that I need to rework my consonant loop. Any suggestions?
    Last edited by willie lee; May 24th, 2012 at 08:43 AM.

  16. #36
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

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

    Quote Originally Posted by willie lee View Post
    I did what you suggested and all worked well concerning the proper counting of vowels and consonants. However, when I entered the target sentence (Welcome to Foothill.) the count of vowels was correct, but the count of consonants was not. My loop counted 13 consonants. There should be only 10. I checked the output for each word separately and the count was correct. Why does my target sentence not work? I will fiddle with my program again tomorrow. Thanks for your help. I am getting very close to making my program work correctly.
    The problem is that the way jcrosby10 suggested spaces will be counted as consonants. If I were you I would first remove the spaces (or any other non-letter characters) from the entered String and then count the vowels. After that the only thing you need to do is subtract the number of vowels from the String's length (which after the removal will be the number of letters only). To remove the spaces (or any other character) from the String you can use the String's replaceAll method. If you google it you will find many examples of how it works.
    Hope I was clear.

  17. #37
    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 do not understand how I should implement a separate counter for each thing I am counting. Could you please give me one example. In the meantime, I implemented aussiemcgr's suggestion. All worked well, but the consonant count was still wrong concerning the target sentence (Welcome to Foothill.). The consonant count was 13 whereas it should have been 10. I am getting very close to making this program work correctly. I no longer get the 1 vowel and 2 consonants count which was driving me crazy. Thanks again for all the support.

  18. #38
    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 do not understand how I should implement a separate counter for each thing I am counting
    Use a separate counter variable for each thing you want to count,

      int cntr1 = 0;  // counter for type 1
      int cntr2 = 0;  // counter for type 2
    ...
      if(want to count type 1) {
         cntr1++;  // count type 1
      }
    ...
      if(want to count type 2) {
         cntr2++;  // count type 2
      }
    If you don't understand my answer, don't ignore it, ask a question.

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

    willie lee (May 27th, 2012)

  20. #39
    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 example. My program is counting white space and punct. I did the following:
    [code = java]
    System.out.println(" Enter the String. ");
    String s1 = input.nextLine();

    s1 = toUpperCase();
    System.out.println("=====RESULT=====" + s1);
    s1 = to LowerCase();
    System.out.println("=====RESULT=====" + s1);
    s1 = s1.replaceAll("\\p{Punct} + ", " ");
    System.out.println("=====RESULT=====" + s1);
    System.out.println(" String s1 ");
    [/code]
    Output
    [code = java]
    Enter the String
    Welcome to Foothill.
    =====RESULT=====WELCOME TO FOOTHILL.
    =====RESULT=====welcome to foothill.
    =====RESULT=====welcome to foothill.
    String s1
    There are 7 vowels.
    There are 13 consonants.
    [/code]
    The replaceAll method, as I used it, does not get rid of the white space and punct. as I wanted. What did I do wrong and how can I correct it.

  21. #40
    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

    For testing write a small (~ 10 lines) program that defines a String and uses the replaceAll() method and prints out the results. Then work on the regular expression used by the replaceAll() method until you find one that does what you want. If you have problems, Post the small program and its results with your questions.
    If you don't understand my answer, don't ignore it, ask a question.

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

    willie lee (May 28th, 2012)

  23. #41
    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 finally got the program to work correctly.
    String s1;
     
    System.out.println(" Enter the String ");
    s1 = input.nextLine();
     
    s1 = toUpperCase();
    System.out.println("=====RESULT=====" + s1);
    s1 = toLowerCase();
    System.out.println("=====RESULT=====" + s1);
    String nonStrippedString = s1;
    s1 = nonStrippedString.replace( ".", "");
    s1 = s1.replaceAll(" [^A-Za-z]", "");
    System.out.println(" String s1 ");
    Enter the String
    Welcome to Foothill.
    =====RESULT=====WELCOME TO FOOTHILL.
    =====RESULT=====welcome to foothill.
       String s1
       There are 7 vowels.
       There are 10 consonants.
    I have tested it by including different symbols and numbers. The vowels and consonants are counted correctly. Now, I must display the String in the reverse order. If I get stuck I will contact you for assistance. Again, thanks to you and all the other interested parties. I would put the solved symbol on this program, but I do not see it on this page.
    Last edited by willie lee; June 16th, 2012 at 11:50 AM.

  24. #42
    Junior Member
    Join Date
    Jun 2012
    Posts
    20
    Thanks
    1
    Thanked 3 Times in 3 Posts

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

    Just try using && instead of || in the second for loop.I think that should solve a major part of the problem.

  25. The Following User Says Thank You to Karthik Prabhu For This Useful Post:

    willie lee (June 19th, 2012)

Page 2 of 2 FirstFirst 12

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