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

• February 20th, 2013, 02:16 PM
Potat
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:
Code :

``` 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.
Code :

```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. :)
• February 20th, 2013, 02:35 PM
Norm
Re: Using a Loop and Nested Ifs to Count Number of Vowels in a String
Quote:

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?
• February 20th, 2013, 02:52 PM
Potat
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.
• February 20th, 2013, 03:08 PM
Norm
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.
• February 20th, 2013, 03:46 PM
Potat
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.
• February 20th, 2013, 03:52 PM
Norm
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.
• February 20th, 2013, 04:07 PM
Potat
Re: Using a Loop and Nested Ifs to Count Number of Vowels in a String
Quote:

Originally Posted by Norm
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.
• February 20th, 2013, 04:10 PM
Norm
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?
• February 20th, 2013, 04:13 PM
Potat
Re: Using a Loop and Nested Ifs to Count Number of Vowels in a String
Yes, we can do that.
• February 20th, 2013, 04:17 PM
Norm
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?
• February 20th, 2013, 04:25 PM
Potat
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?
• February 20th, 2013, 04:37 PM
Norm
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
• February 20th, 2013, 04:41 PM
Potat
Re: Using a Loop and Nested Ifs to Count Number of Vowels in a String
You're saying to do this:
Code :

```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.
• February 20th, 2013, 04:54 PM
Norm
Re: Using a Loop and Nested Ifs to Count Number of Vowels in a String
Leave off the else
• February 20th, 2013, 05:11 PM
Potat
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!
• February 20th, 2013, 05:33 PM
Norm
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.
• February 21st, 2013, 08:08 PM
SunshineInABag
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.
• February 21st, 2013, 08:35 PM
Norm
Re: Using a Loop and Nested Ifs to Count Number of Vowels in a String
Quote:

take the inputted String and convert it to a char array
FYI The String class's charAt() method does that.