Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

If only one vowel is entered in the string, then that vowel should be diplayed on console

 
priyaa singhaniaa
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.

I am a new bie in programming.

The answer for my question may look very simple to you.

This my current code.
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.lang.String;
import java.io.*;
import java.util.*;

public class CountVowels {

public static void main(String args[]) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the String:");
String text = bf.readLine();
int count = 0;
char c='a';

for (int i = 0; i < text.length(); i++) {
c = text.charAt(i);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
count++;

}

}
if (count == 1)
System.out.println("there is only one vowel in the string you entered and that is --------");
else
System.out.println("There are" + " " + count + " " + "vowels");
}
}
------------------------------------------------------------------------------------------------------------------------------------

The requirement is , when i run this program giving the string "abcd"(excluding quotes). the program should give me the output as
"there is only one vowel in the string you entered and that is a"

Please suggest me the required changes that are to be made to the code.

Thanks
 
Himanshu Kansal
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Use CODE Tags
2. Keep only the second import. Others are redundant.
3. Play safe. Do not initialize "c" to "a". Use something else.
4. A simple logic that you can use is replacing all the alphabets other than vowels by "" and output the length of the remaining string.
5. use: instead

There can be other improvements too.

Regards
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) Have a char or String variable hold the value of the last vowel found, and use this result if count == 1.
2) Simplify your search by changing your text String to all upper case or lower case (player's choice) and searching this string for vowels.
3) If you want to blow your teacher away with a little bit of overkill, use regex (actually not recommended for this simple exercise):
 
Campbell Ritchie
Sheriff
Pie
Posts: 48952
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree with the suggestion about code tags. And yes, some of those imports are unnecessary. But you still have the problem that your code will blithely take "rhythmic" and tell you it contains 1 vowel (correct) and it is "c"

Now work out how to get it to tell me the only vowel in rhythmic is "i"!
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pete stein wrote:

I don't know if it's faster, but I do prefer the following a bit more:
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:
I don't know if it's faster, but I do prefer the following a bit more:

Thanks! It certainly appears cleaner.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48952
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you managed it yet, with or without regular expressions?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic