The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
February 2002

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

code

Posted by ping203 on February 13, 2002 at 12:58 PM

I tested it and it works

import java.io.*;

public class CountVowels

{
public static void main (String[] args) throws IOException

{
BufferedReader stdin = new BufferedReader
(new InputStreamReader(System.in));

System.out.println ("Enter a string: ");
String inString = new String(stdin.readLine());

String stringToRead = inString;
char aeiouAEIOU;

int aCount = 0;
int eCount = 0;
int iCount = 0;
int oCount = 0;
int uCount = 0;
int otherletters = 0;
for (int count = 0;count < stringToRead.length();count++) {

aeiouAEIOU= stringToRead.charAt(count);

switch(aeiouAEIOU) {
case 'a':case 'A':
aCount = aCount + 1;
break;
case 'e':case 'E':
eCount = eCount + 1;
break;
case 'i':case 'I':
iCount = iCount + 1;
break;
case 'o':case 'O':
oCount = oCount + 1;
break;
case 'u':case 'U':
uCount = uCount + 1;
break;
default: //just add default after the last case
otherletters++; //if the char is not aeiouAEIOU, then
break; //otherCount++ will be executed
}
}
System.out.println ("Total A=" + aCount);
System.out.println ("Total E=" + eCount);
System.out.println ("Total I=" + iCount);
System.out.println ("Total O=" + oCount);
System.out.println ("Total U=" + uCount);
//original codes dont have + otherletters
System.out.println ("Total Other=" + otherletters);
}
}




Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us