Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

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

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


    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);
    
    		}
    
    
    	}
    


Welcome!

It looks like you're new here. Sign in or register to get started.

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,009 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,009 WhiskeyGoblin
    ✭✭✭


    Cheers man really appreciate it!


Welcome!

It looks like you're new here. Sign in or register to get started.
Advertisement