Solved

How to select non-vowel characters

Posted on 2006-10-30
3
676 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

636 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