wood burning stoves*
The moose likes Beginning Java and the fly likes If only one vowel is entered in the string, then that vowel should be diplayed on console Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "If only one vowel is entered in the string, then that vowel should be diplayed on console " Watch "If only one vowel is entered in the string, then that vowel should be diplayed on console " New topic
Author

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

priyaa singhaniaa
Greenhorn

Joined: Oct 26, 2008
Posts: 2
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

Joined: Jul 05, 2009
Posts: 257
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


Experience and talent are independent of age
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Oct 13, 2005
Posts: 37902
    
  22
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

Joined: Oct 27, 2005
Posts: 19649
    
  18

pete stein wrote:

I don't know if it's faster, but I do prefer the following a bit more:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Oct 13, 2005
Posts: 37902
    
  22
Have you managed it yet, with or without regular expressions?
 
wood burning stoves
 
subject: If only one vowel is entered in the string, then that vowel should be diplayed on console
 
Similar Threads
Count the number of vowels, words, and sentences.
Part of program not printing output
Overhead IO : The cryptic case of the extra bytes
translate to pig latin!? completely lost....
Quick String to <char> Array Conversion