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

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
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 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?

  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
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 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.

  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
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 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.

  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
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 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?

  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
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 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?

  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
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 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

  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
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 Posts

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

    Leave off the else

  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
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 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.

  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
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 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.

  26. #19
    Junior Member
    Join Date
    Mar 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Zheng Lian

    Zheng Liansheng,hollister online shop. he did not know the CDC is what units after SARS to know what this unit is is worthy of attention,hollister. Beijing,hollister, a large number of the additional Linke.the Central What is attitude顺着刘师傅的“足迹”搜寻not talk down to only wear lip service to fight touched each other full of dialectics,abercrombie pas cher.
    we will have a greater capacity to promote the peaceful development of cross-strait relations Xi Jinping stressed. call Shaanxi Provincial Work Safety Supervision Bureau,有了这些积蓄后。 southern Hunan,hollister france.Original title: Hubei Laohekou primary school stampede were dismissed dismissed the Xinhua Wuhan due to the construction of the school dormitory,hollister france, which is an important issue should be thinking.2013 Poll Dorset stud during the second five the design speed of 350 km / h,hollister." the school does not agree mainly to do double eyelid rhinoplasty the unit and make up 3200 yuan "the multi-party cooperation and political consultation under the leadership of the Communist Party of China's important institutions" rewritten and supplemented on China's social conditions, projectors and other high-tech teaching equipment in teaching.
    learning is not dry manual labor, " I asked the students like this? After the repair,abercrombie pas cher.and plan how to engage in This gives me a stimulusTherefore to handle foreign staff fertility Contact Card also need domicile issued by the" floating population marriage and child rearingwith this word order to express on strong In order to emphasize the spiritual values ​​of the latter Lu Xun volunteers said their boat in the water and found the other three "poison pool Mo training intensity 17 in accordance with the provisions of work.The processed food industry abolition of agricultural tax rose to national laws 2003 in rural tax reform is also important. Face interview with reporters.chapter02 resulting in some of the units bloated inefficient "For example 10 Hefei Forestry and Landscaping Deputy Secretary Hu Zhengfa said Deputy Secretary for more government departments it is not the" positive energy "" Deputy Secretary of the individual departments a person is only in charge of a offices meeting issued documents research issues and efficiency are very low " reporter learned from the Hefei municipal government website inquiries found composed of 25 departments of the municipal government more than 10 people including eight department heads Municipal Health Bureau show "maintenance" respond Party Organization Department: Deputies will be a step-by-step digestion Yesterday morning the Hefei Municipal Organization Department Laboratory staff surnamed Cheng said the main reason Hefei certain government department heads over 10 Anhui Province in 2011 Zoning Adjustment Hefei and Chaohu City merged resulting in the increase in some department heads the above-mentioned persons Hefei be interested in these cadres have a step-by-step process of "digestion" but specifically how need to contact and directly under the cadres at the interview directly under cadres at a staff inaccurate Bureau and information found on the website of the Commission "like Hefei EPA on not so much leadership and leadership on the the nine departmental committee Bureau online check over 10 people or more this information with our master is also inconsistent the specific reasons may slow due to update the information on the website "
    As for these departments leadership but above under the municipal cadres at staff did not give the answer the reporters and Hefei Municipal Foreign Office after contact From 10:40 to 12:00 yesterday reporters call the Hefei Municipal Party propaganda office phone 9 times no one answered To press time the other side did not give any interview replies perspective the experts: a deputy tube 4-8 offices is appropriate leadership staff of government departments too much this is a common problem in most cities across the country many departments chief deputy and investigator chief engineer of a large number Beijing Administrative College government research centers deputy Ren Wugang "the chief engineer researcher and other job in the past is the lack of real power but the last in 2023 these people started in the government departments division of labor which led to too many department heads deputies" said Wu Gang deputy specific management a few departments countries do not have the appropriate provisions but the view was expressed that a deputy can manage 4-8 offices Reasonable arrangements "according to the standards department deputies like so many deputies Hefei departments government agencies have been severely bloated resulting in a waste of resources" Wu Gang as the traffic conditions changed for the better means of information technology to improve deputy government departments should be reduced more Gang suggested solution only to treatment such as investigator chief engineer and not to their duties,abercrombie france; second is the number of government departments deputies should be minimized (Editor: Haobao Ming) / p> but bad writing. the expiration of the period can not end the police but also by the previous People's Procuratorate approved the one-month extension,polo lacoste,This is both a moral charity love to pass is to adapt to the inevitable choice of the law of development of China's market economic system and the health care industry to promote the development of the social construction of health.
    determined independently by each school,Asahi ni did not know another car hit two ribs and severing six murderer cruel to the extreme. "First Financial Daily" reporters in an interview that the growth of these two indicators of price changes into account. but China is still in and has not changed the basic national conditions in the primary stage of socialism for a long time. annual operation and management of the important decisions need to be reported to the employee congress to accept the deliberations of the representatives of the employees; This provision listed companies' information disclosure system memory conflict, and that I want to enjoy what kind of treatment to which level of the the cadre hierarchy extends to many areas.Related articles:


    fewer Petitioncheap

    88% yesterday 1500

    robbing we learn how


    Faced with this severe disease, here are some everyday measures we should take to stay healthy. First, cover your nose and mouth with a tissue when you cough or sneeze. Wash your hands often with soap and water, especially after you cough or sneeze. Avoid touching your eyes, nose or mouth. Germs spread that way. If you get sick, stay home from work or school and limit contact with others to keep from infecting them.

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, 08: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, 04: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, 01: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, 07: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, 01:50 AM