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

Thread: Program to find how many letters start with vowels

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    7
    My Mood
    Bored
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Program to find how many letters start with vowels

    The program's output is n't correct

    Here's the code......

     
    import java.io.*;
    class Vowels   
     {
        void Input()throws IOException
             {
                 BufferedReader a = new BufferedReader(new InputStreamReader(System.in));
                 System.out.println("Enter your Sentence");
                 String b = a.readLine();
                  int c = b.length();
                  int d=0,f=0;
                  char e ; 
                  for(;d<c;d++)
                     {
                        e = b.charAt(d);
                         if((e=='A')||(e=='E')||(e=='I')||(e=='O')||(e=='U'))  \\Used for letters at beginning of Sentence 
                              ++f;
                          if(((e=='a')||(e=='e')||(e=='i')||(e=='o')||(e=='u'))&&((d-1)==' ')&(d!=0))
                               ++f;
                       }
                    System.out.print(f);
                }
          }

    I will be happy if those lengthy if conditions is shortened.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Program to find how many letters start with vowels

    The program's output is n't correct
    For a given input, what do you receive and what do you expect? Please provide as much detail as possible

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    11
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Program to find how many letters start with vowels

    You code don't work because you forget of to write b.charAt(d-1). You wrote only (d-1).

                      if(((e=='a')||(e=='e')||(e=='i')||(e=='o')||(e=='u'))&&(b.charAt(d-1)==' ')&(d!=0))

    You can reduce you code this way:

     
         void Input()throws IOException
         {
             BufferedReader a = new BufferedReader(new InputStreamReader(System.in));
             System.out.println("Enter your Sentence");
             String[] b = a.readLine().toLowerCase().split("[ ]+");
             int f = 0;
             for(String s: b)
             {
            	 if(s.matches("[aeiou].*"))
            		 f++;
             }
     
             System.out.println(f);
     
            }

    In line

     String[] b = a.readLine().toLowerCase().split("[ ]+");

    I divide the sentence in words and I passed all to lower case. The separator is one or more spaces.

    In line

     if(s.matches("[aeiou].*"))

    I use regular expression. If word start with vowels f is increment. The .* mean any caracter.

    I was helped or confused?

  4. The Following User Says Thank You to tiagoufrj For This Useful Post:

    Lokesh (February 14th, 2011)

  5. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Smile Re: Program to find how many letters start with vowels

    What about the startsWith(String prefix) method?

    String (Java Platform SE 6)

    Are you asking if each letter is a vowel or if each word begins with a vowel?

    I'm not familiar with the matches and the Pattern class.

    What exactly is that doing?

    I'm thinking it's saying that if it has any of the chars in "aeiou" to increment f.

    If so, that beats my idea.

    Now at least I know what that method in the String class really does.
    Last edited by javapenguin; February 14th, 2011 at 01:33 AM.

  6. #5
    Junior Member
    Join Date
    Feb 2011
    Posts
    11
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Program to find how many letters start with vowels

    >What about the startsWith(String prefix) method?

    You can to use startsWith. But you have to use for each letter. For example:

     
     if(s.startsWith("a") || s.startsWith("e") || s.startsWith("i") || s.startsWith("o") || s.startsWith("u"))
        f++;

    >Are you asking if each letter is a vowel or if each word begins with a vowel?

    On my code I see if each word begins with a vowel.

    >What exactly is that doing?

    In a regular expression we have some rules, for examples:

    [ ] means that the character is a space or \n or \r
    [ ]+ means one or more (spaces or \n or \r)
    [aeiou] means that the character is a or e or i or o or u
    . means one any character
    .* means zero or one or more any character

    So

    I use the regular expresssion ("[ ]+") for divide sentence in words. And I Use ("[aeiou].*") for match any word that begins with a or e or i or o or u follow of any character. How I divide this sentence in words each word don't have spaces.

    understand?

Similar Threads

  1. Extracting letters as variables
    By TH1 in forum Java Theory & Questions
    Replies: 3
    Last Post: February 11th, 2011, 07:22 AM
  2. [SOLVED] Replacing letters in a string NOT WORKING.
    By sp11k3t3ht3rd in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 14th, 2011, 06:38 PM
  3. newbie start java program with switches
    By fortune2k in forum Java Theory & Questions
    Replies: 4
    Last Post: November 1st, 2010, 05:13 PM
  4. Counting Vowels and Prepositions in a text file
    By maybach230 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 27th, 2010, 07:36 PM
  5. How to remove letters
    By noobish in forum Java Theory & Questions
    Replies: 13
    Last Post: October 3rd, 2009, 10:36 PM