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 18 of 18

Thread: Using a Loop and Nested Ifs to Count Number of Vowels in a String

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    10
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Using a Loop and Nested Ifs to Count Number of Vowels in a String

    Hey there, I need some advice on how to complete some code. The code needs to take an inputted string from the user, then print out the number of vowels in the string. The challenge here is that I'm only allowed to use nested if statements (so no chained if statements or compound conditions). Here's what I have:
    	public static void problem() {
     
    		Scanner keyboard = new Scanner(System.in);
    		System.out.println("Enter a string and I will tell you the number of vowels that appear.");
    		String input = keyboard.nextLine();
    		String lowerCaseInput = input.toLowerCase();
    		int stringLength = input.length();
    		int numberOfOccurancesOfVowels = 0;                                                 
    		for (int count = 0; count < stringLength; count++)                     
    		{                           
     
    		}
    			System.out.println("There are " + numberOfOccurancesOfVowels + " vowels in " + input + ".");
     
    	}

    I don't understand how using nested if statements can complete the task. I know what they are, and how to use them, but I have no idea on how to apply them to this situation. They should be placed within the loop, but what should the conditions be? I was thinking of putting in the following if's within the loop, but it would be both time consuming and extremely long-winded. I'm 100% certain that there is a simpler way.
    if ((lowerCaseInput.charAt(count) != 'b'))
    {
    if ((lowerCaseInput.charAt(count) != 'c'))
    {
    if ((lowerCaseInput.charAt(count) != 'every other char except for a,e,i,o,u'))
    {
    numberOfOccurancesOfVowels++;
    }
    }
    }
    Thank you for any help you provide. Please don't say anything along the lines of "check out The Java™ Tutorials" or "go buy a Java textbook and read it." I would like some actually helpful information from people who have a better grasp of what to do than I. Again, thanks for reading and any advice.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Using a Loop and Nested Ifs to Count Number of Vowels in a String

    I'm only allowed to use nested if statements
    What methods and techniques can you use? I see the String class's charAt() and toLowerCase().
    Can you use other String class methods? Can you have other Strings with reference data?
    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:

    Potat (February 20th, 2013)

  4. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    10
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Using a Loop and Nested Ifs to Count Number of Vowels in a String

    I'm assuming we can use everything else, but my professor specifically said not to use compound conditions or chained if statements. Yes, we can use other Strings that hold reference data, this is just what I came up with so far.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Using a Loop and Nested Ifs to Count Number of Vowels in a String

    An easy way to see if a character is a member of a group of characters is to make a String of the group of characters and use a String method to see if the char to be checked is in the other String. One method does that.
    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:

    Potat (February 20th, 2013)

  7. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    10
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Using a Loop and Nested Ifs to Count Number of Vowels in a String

    I think you're talking about the "compareToIgnoreCase" method, but I still don't know how to incorporate it into the nested If statements to get the number of vowels. If I was allowed to use chained If statements or compound conditions then that would be a valid solution.

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Using a Loop and Nested Ifs to Count Number of Vowels in a String

    What I'm suggesting takes one if statement to test the results of the search for the single letter being in a String of desired letters.

    If the assignment's requirements are that a tree of nested if tests be made to determine some condition (a letter is in a group) then my solution bypasses all of that.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Potat (February 20th, 2013)

  10. #7
    Junior Member
    Join Date
    Jan 2013
    Posts
    10
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Using a Loop and Nested Ifs to Count Number of Vowels in a String

    Quote Originally Posted by Norm View Post
    If the assignment's requirements are that a tree of nested if tests be made to determine some condition (a letter is in a group) then my solution bypasses all of that.
    Yep this is exactly what the assignment is to do.

  11. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Using a Loop and Nested Ifs to Count Number of Vowels in a String

    Are you allowed to increment the counter in more than one place?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Potat (February 20th, 2013)

  13. #9
    Junior Member
    Join Date
    Jan 2013
    Posts
    10
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Using a Loop and Nested Ifs to Count Number of Vowels in a String

    Yes, we can do that.

  14. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Using a Loop and Nested Ifs to Count Number of Vowels in a String

    Would a series of if tests for the each of the vowels work?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Potat (February 20th, 2013)

  16. #11
    Junior Member
    Join Date
    Jan 2013
    Posts
    10
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Using a Loop and Nested Ifs to Count Number of Vowels in a String

    Yes, that should work, but how would I go about doing that?

  17. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Using a Loop and Nested Ifs to Count Number of Vowels in a String

    Make a simple if statement to test if the letter is 'a' and increment the count if true.
    Make 4 copies of that changing the letter being tested for in each if test for the next vowel
    If you don't understand my answer, don't ignore it, ask a question.

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

    Potat (February 20th, 2013)

  19. #13
    Junior Member
    Join Date
    Jan 2013
    Posts
    10
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Using a Loop and Nested Ifs to Count Number of Vowels in a String

    You're saying to do this:
    for (int count = 0; count < stringLength; count++)                     
    		{                                                                            
    			if (lowerCaseInput.charAt(count) == 'a')                                                                
    			{
    				numberOfOccurancesOfVowels++;
    			}
    			else if (lowerCaseInput.charAt(count) == 'e') 
    			{
    				numberOfOccurancesOfVowels++;
    			}
    			else if (lowerCaseInput.charAt(count) == 'i') 
    			{
    				numberOfOccurancesOfVowels++;
    			}
    			else if (lowerCaseInput.charAt(count) == 'o')
    			{
    				numberOfOccurancesOfVowels++; 
    			} 
    			else if (lowerCaseInput.charAt(count) == 'u')
    			{
    				numberOfOccurancesOfVowels++;
    			}
    		}
    That's the logical way to do it, but that's also using chained if statements as opposed to using nested. I don't know what to do.

  20. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Using a Loop and Nested Ifs to Count Number of Vowels in a String

    Leave off the else
    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:

    Potat (February 20th, 2013)

  22. #15
    Junior Member
    Join Date
    Jan 2013
    Posts
    10
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Using a Loop and Nested Ifs to Count Number of Vowels in a String

    Yeah that would serve the same function. Regardless, it wouldn't be using just nested if statements. It's okay. I'll figure it out. Thanks for the help!

  23. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Using a Loop and Nested Ifs to Count Number of Vowels in a String

    Then you need to build a binary tree. Test a condition at a node, go left if true, go right if false.
    Take a piece of paper and a pencil and work out the logic.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #17
    Member
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    54
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default Re: Using a Loop and Nested Ifs to Count Number of Vowels in a String

    Could you not take the inputted String and convert it to a char array? After, you could use a loop that contains nested if statements, where it checks whether each element is a vowel. If so, it increments your counter. The moment a vowel is found, then there's no point in checking the other if-statements, so you can use the keyword, "continue;" (without the " "). It's essentially creating a binary tree in Java. I'm assuming this approach would be allowed since you did not specify whether arrays could be used or not.

  25. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Using a Loop and Nested Ifs to Count Number of Vowels in a String

    take the inputted String and convert it to a char array
    FYI The String class's charAt() method does that.
    If you don't understand my answer, don't ignore it, ask a question.

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 for loops to count vowels/consonants usinf the logic of the main
    By willie lee in forum Loops & Control Statements
    Replies: 41
    Last Post: June 18th, 2012, 05:01 AM
  3. [SOLVED] Problem using the length of a string to bound the number of iterations of a for loop
    By dtitt3 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 3rd, 2011, 02:44 PM
  4. Count Number of Each Letter in Given Word?
    By TheBattousaixx in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 11th, 2011, 08:55 PM
  5. Where I'm I wrong? I need to do a count of the number of each element in an array
    By NavagatingJava in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 21st, 2011, 02:50 AM