Count Vowels


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: Count Vowels

  1. #1
    Join Date
    Mar 2008
    Posts
    6

    Count Vowels

    Hey again guys :P Sorry for needing all this help i have another question i need some help with! Thanks for any help in advance. Heres the question.

    “THE QUALITY OF MERCY IS NOT STRAINED, IT DROPPETH AS THE GENTLE RAIN FROM HEAVEN ON THE PLACE BENEATH.”
    This is the opening sentence of Portia’s speech in Shakespeare’s “The Merchant of Venice”.

    Write a program that reads a data file containing this sentence (Use Upper case letters!) and counts the number of times each of the five vowels a, e, i, o and u occur in the sentence.

  2. #2
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    Welcome to DevX

    So, basically, you just need something that counts the number vowels in a sentence, right?
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  3. #3
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    Try this
    Code:
    //countVowels()
    
    int countVowels(String text) {
        int count = 0; // start the count at zero
        // change the entire string to uppercase
        text = text.toUpperCase();
      
        for (int i = 0; i < text.length(); i++) {
            char c = text.charAt(i);
            if (c=='a' || c=='e' || c=='i' || c=='o' || c=='u') {
                count++;
            }
        }
        return count;
    }
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  4. #4
    Join Date
    Mar 2008
    Posts
    6
    Thanks for the starter But i need to get it to read the text from a data file.
    Since there is no String args[] statement where would i tell it to read the data file?

    Thanks again

  5. #5
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    That was just an example. Just change where the text is coming from.

    You must have gone over, in your class, how to open and read from a text file, right?
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  6. #6
    Join Date
    Mar 2008
    Posts
    6
    Hey i have this code but i am getting some logic errors. The code is only reading the first word from my text file! How do i get around this? This code is probably a nightmare but i strictly cant see whats wrong with it Thanks for any help in advance!
    Code:
    import java.io.File;
    import java.util.Scanner;
    import java.io.IOException;
    class Portia
    
    {
    public static void main(String args[])
    throws IOException
      {
        
        
        Scanner PortiaScanner = new Scanner(new File("Portia.txt"));
          
       String txt;
       int vowels=0;
       int letter=0;
       int spaces=0;
       txt  = PortiaScanner.next();
       System.out.println("\nThe String is    :"+txt);
       int n=txt.length();
       for(int i=0;i<n;i++)
        {
          if(txt.charAt(i)=='a'||txt.charAt(i)=='A')
          {
           ++vowels;
          }
          else if(txt.charAt(i)=='e'||txt.charAt(i)=='E')
          {
           ++vowels;
          }
          else if(txt.charAt(i)=='i'||txt.charAt(i)=='I')
          {
          ++vowels;
          }
          else if(txt.charAt(i)=='o'||txt.charAt(i)=='O')
          {
          ++vowels;
          }
          else if(txt.charAt(i)=='u'||txt.charAt(i)=='U')
          {
          ++vowels;
          }
          else if(txt.charAt(i)==' ')
          {
          ++spaces;
          }
          else
          {
          ++letter;
          }
        }
        System.out.println("\nThe Vowels are    :"+vowels);
        System.out.println("\nThe Spaces are    :"+spaces);
        System.out.println("\nThe Letters are    :"+letter);
    }
    }

    Thanks people
    Last edited by epilogue; 03-06-2008 at 05:01 AM.

  7. #7
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    What are the errors that you are getting and what lines of code are throwing them?
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  8. #8
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    You are getting only one token from the file input ... you need to keep doing your evaluation by using a

    while (PortiaScanner.hasNext() )
    {
    token processing code
    }

    loop to process each token until all the tokens are gone.

Similar Threads

  1. Count Vowels
    By RichardGR in forum VB Classic
    Replies: 3
    Last Post: 12-28-2007, 02:08 PM
  2. Query works in Access, but not VB .NET
    By corygibbons in forum Database
    Replies: 6
    Last Post: 07-11-2007, 09:30 PM
  3. Help Semantic Tableaux Algorithm
    By dina86 in forum Java
    Replies: 0
    Last Post: 04-20-2007, 03:13 PM
  4. need to return total record count, but here is the trick...
    By barbarosa80503 in forum VB Classic
    Replies: 2
    Last Post: 10-28-2005, 04:33 PM
  5. count the number of vowels in a string
    By peskygal in forum VB Classic
    Replies: 6
    Last Post: 02-22-2005, 07:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center