Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Don't suppose anyone could tell me why this keeps looping?

  • 21-11-2012 10:49PM
    #1
    Registered Users, Registered Users 2 Posts: 4,010 ✭✭✭


    Just cannot see where I'm going wrong.
    {
    
    	public static void main(String args[])
    	{
    		//Setting up String
    		String word = new String("Hello");
    		System.out.println("The word is " + word);
    
    		//Declaring and intilizing ints
    		int length	= word.length();
    		int count 	= 0;
    		int VowelA= 0, VowelE=0, VowelI=0, VowelO=0, VowelU=0, numberofVowels=0, numberC=0;
    
    		//Setting up while loop and parameters
    
    		while(count<length)
    		{
    			if(word.charAt(count)=='a')
    			{
    				numberofVowels++;
    				VowelA++;
    				count++;
    			}
    			else if (word.charAt(count)=='e')
    			{
    				numberofVowels++;
    				VowelE++;
    				count++;
    			}
    			else if (word.charAt(count)=='i')
    			{
    				numberofVowels++;
    				VowelI++;
    				count++;
    			}
    			else if (word.charAt(count)=='o')
    			{
    				numberofVowels++;
    				VowelO++;
    				count++;
    			}
    			else if (word.charAt(count)=='u')
    			{
    				numberofVowels++;
    				VowelU++;
    				count++;
    			}
    
    
    		System.out.println("The total number of As is " + VowelA);
    		System.out.println("The total number of Es is " + VowelE);
    		System.out.println("The total number of Is is " + VowelI);
    		System.out.println("The total number of Os is " + VowelO);
    		System.out.println("The total number of Us is " + VowelU);
    
    		}
    
    
    	}
    


Comments

  • Closed Accounts Posts: 6,281 ✭✭✭Ricky91t


    Once the loop reaches a non-vowel you'll never increment count meaning the while loop will never meet the 'exit' condition.

    Say for 'Hello'

    count = 0;

    So the character is H.
    while(count<length)
    		{
    			if(word.charAt(count)=='a')
    			{
    				...
    			}
    			else if (word.charAt(count)=='e')
    			{
    				...
    			}
    			else if (word.charAt(count)=='i')
    			{
    				...
    			}
    			else if (word.charAt(count)=='o')
    			{
    				...
    			}
    			else if (word.charAt(count)=='u')
    			{
    				...
    			}
                            ...
                            //gets to here, no vowels are present, so we'll loop again
                            //count still = 0;
                            //So H is checked again and again and again, infinitely!
    		}
    
    
    
    

    To fix it increment count before the closing bracket of the while loop, and no where else.

    This means you'll always cycle through whether or not the current character is a vowel.


  • Registered Users, Registered Users 2 Posts: 4,010 ✭✭✭WhiskeyGoblin


    Changed it to this, but presume I've still misplaced the increment.
    if(word.charAt(count)=='a')
    			{
    				numberofVowels++;
    				VowelA++;
    
    			}
    			count++;
    


  • Closed Accounts Posts: 6,281 ✭✭✭Ricky91t


    Sorry, I meant at the end of all the ifs and else ifs.


  • Registered Users, Registered Users 2 Posts: 4,010 ✭✭✭WhiskeyGoblin


    Cheers man really appreciate it!


Advertisement