Solved

How to select non-vowel characters

Posted on 2006-10-30
3
660 Views
Last Modified: 2012-08-13
I have the following code that checks for lower case vowels in a string, counts them and prints them. How could I ammend this code to count any character that is not A, I, E, O, U, a, i, e, o or u?

Thanks!
0
Comment
Question by:msheppard74
  • 2
3 Comments
 

Author Comment

by:msheppard74
ID: 17836566
Here is the code

import java.util.*;

public class SheppardPP515
{

      public static void main (String[] args)
      {
      
            int a = 0;
            int e = 0;
            int i = 0;
            int o = 0;
            int u = 0;
            int nonVowels = 0;
            
            String message;
            
            Scanner scan = new Scanner (System.in);
            
            System.out.println ("enter your string now:");
            
            message = scan.nextLine();
            
            
                  for (int n = 0; n < message.length(); n++)
                        {
                              if (message.charAt(n) == 'a')
                                          a++;
                              if (message.charAt(n) == 'e')
                                          e++;
                              if (message.charAt(n) == 'i')
                                          i++;
                              if (message.charAt(n) == 'o')
                                          o++;
                              if (message.charAt(n) == 'u')
                                          u++;            
                        }
                                    
                  if (a != 0)
                        System.out.println("There are " + a + " a's.");
                  if (e != 0)      
                        System.out.println("There are " + e + " e's.");
                  if (i != 0)      
                        System.out.println("There are " + i + " i's.");
                  if (o != 0)      
                        System.out.println("There are " + o + " o's.");
                  if (u != 0)      
                        System.out.println("There are " + u + " u's.");
0
 

Accepted Solution

by:
msheppard74 earned 0 total points
ID: 17836807
I just ammeded the code as follows. I answered this myself.

Thanks

import java.util.*;

public class SheppardPP515
{

      public static void main (String[] args)
      {
      
            int a = 0;
            int e = 0;
            int i = 0;
            int o = 0;
            int u = 0;
            int nonVowels = 0;
            
            String message;
            
            Scanner scan = new Scanner (System.in);
            
            System.out.println ("enter your string now:");
            
            message = scan.nextLine(); //user enters String now
            
            
                  for (int n = 0; n < message.length(); n++) // this for loop determines how many of each
                        {                                       // lower case vowels there are in the string.
                              if (message.charAt(n) == 'a')
                                          a++;
                              if (message.charAt(n) == 'e')
                                          e++;
                              if (message.charAt(n) == 'i')
                                          i++;
                              if (message.charAt(n) == 'o')
                                          o++;
                              if (message.charAt(n) == 'u')
                                          u++;
                              
                              if (message.charAt(n) != 'a')
                                 if(message.charAt(n) != 'e')
                                          if(message.charAt(n) != 'i')
                                                if(message.charAt(n) != 'o')
                                                      if(message.charAt(n) != 'u')
                                                            if(message.charAt(n) != 'A')
                                                                  if(message.charAt(n) != 'E')
                                                                        if(message.charAt(n) != 'I')
                                                                              if(message.charAt(n) != 'O')
                                                                                    if(message.charAt(n) != 'U')
                                                                                          if(message.charAt(n) != ' ')
                                                                                                 nonVowels++;                        
                        }
                  
                  System.out.println ("Your entered: " + message);
                  System.out.println ();
                                    
                  if (a != 0)
                        System.out.println ("The following is the total inatances of \"a\": " + a);
                  if (e != 0)      
                        System.out.println ("The following is the total instances of \"e\": " + e);
                  if (i != 0)      
                        System.out.println ("The following is the total instances of \"i\": " + i);
                  if (o != 0)      
                        System.out.println ("The following is the total instances of \"i\": " + o);
                  if (u != 0)      
                        System.out.println ("The following is the total instances of \"i\": " + u);

                  System.out.println ();
                        
         if (nonVowels != 0)
                     System.out.println ("And finally, the total amount of non Vowels: " + nonVowels);
            
      }
      
}
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
has22 challenge 11 105
copyEndy  challenge 15 98
word0 challenge 3 101
rhino JavaScript import, load 25 95
Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
This video teaches viewers about errors in exception handling.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question