Solved

How to select non-vowel characters

Posted on 2006-10-30
3
634 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
build html 5 client that connect to indy Tidtcpserver 3 299
Opening a Access Database 5 121
MaxSpan challenge 9 67
Copy Files - Python 7 55
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 …
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

706 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now