Welcome to Dream.In.Code
Become a Java Expert!

Join 150,407 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 950 people online right now. Registration is fast and FREE... Join Now!




Counting Vowels-Help please.

 
Reply to this topicStart new topic

Counting Vowels-Help please.

myra
4 Apr, 2008 - 04:19 AM
Post #1

New D.I.C Head
*

Joined: 4 Apr, 2008
Posts: 25


My Contributions
Hi, I am new to programming and was asked to create a program on counting vowels as part of my assignment. My coding is as below:

CODE
{
                System.out.println("Enter a line of text: ");
            String input = keyboard.nextLine();
            int length = input.length();
                for (int i= 0; i < input.length(); i++)
                {
                    if(        input.charAt(i) == 'a' || input.charAt(i) == 'A' ||
                               input.charAt(i) == 'e' || input.charAt(i) == 'E' ||
                               input.charAt(i) == 'i' || input.charAt(i) == 'I' ||
                               input.charAt(i) == 'o' || input.charAt(i) == 'O' ||
                               input.charAt(i) == 'u' || input.charAt(i) == 'U'          )    
                            length++;
                            System.out.printf("\nNumber of vowels found in line is %d\n", length);
                }
                
        }


However, I can't seem to get my code to work properly:-( Could someone please give me advise on how to improve this coding of mine? Thanks.


User is offlineProfile CardPM
+Quote Post

1lacca
RE: Counting Vowels-Help Please.
4 Apr, 2008 - 04:44 AM
Post #2

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
rename your length variable to vowelcount and initialize it to 0, not input.length()
Also, the System.out.printf("\nNumber of vowels found in line is %d\n", length); should be outside the for loop.
Other than that it could work.
User is offlineProfile CardPM
+Quote Post

myra
RE: Counting Vowels-Help Please.
4 Apr, 2008 - 05:07 AM
Post #3

New D.I.C Head
*

Joined: 4 Apr, 2008
Posts: 25


My Contributions
QUOTE(1lacca @ 4 Apr, 2008 - 05:44 AM) *

rename your length variable to vowelcount and initialize it to 0, not input.length()
Also, the System.out.printf("\nNumber of vowels found in line is %d\n", length); should be outside the for loop.
Other than that it could work.


Thanks for the reply. I've tested my code out again. It seems ok. However, before I get to enter a text for it to count, it displays the result "Number of vowels is 0." Why is this happening? Does this have to do with the loop or something? Thank you so much for reading this.
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Counting Vowels-Help Please.
4 Apr, 2008 - 05:22 AM
Post #4

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
It should be after the closing curly bracket of for, not before it.
CODE

                for (int i= 0; i < input.length(); i++)
                {
                    if(        input.charAt(i) == 'a' || input.charAt(i) == 'A' ||
                             //...
                            
                }
                System.out.printf("\nNumber of vowels found in line is %d\n", length); // HERE


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 07:33PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month