Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to select non-vowel characters

Posted on 2006-10-30
3
Medium Priority
?
704 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
2 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 article will show, step by step, how to integrate R code into a R Sweave document
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

824 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