Solved

How to select non-vowel characters

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
format the code in java 6 93
fizzArray3  challenge 1 88
bigHeights  challenge 13 62
Problem to open text file 11 110
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 …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
The viewer will learn how to implement Singleton Design Pattern in Java.
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…

773 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