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

Jump to content


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.

Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

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

#1 User is offline   abcd_1 

  • Member
  • PipPip
  • Find Topics
  • Group: Members
  • Posts: 88
  • Joined: 01-December 08

Posted 25 July 2009 - 01:36 PM

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

#2 User is offline   groovicus 

  • Hail Groovicus!
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Moderator
  • Posts: 9,522
  • Joined: 05-June 04
  • Gender:Male
  • Location:Centerville, SD

Posted 25 July 2009 - 02:18 PM

If the character is a vowel, then you need to set the value of 'c' to whatever value that is.

For example:
 

if(charAt(i) == 'a') {
   c = charAt(i));
}


Always use braces in if statements, even if there is only one statement inside. From a maintenance standpoint, it will prevent simple errors later.

To get your program to print out the value of c, you just need to print it out, just like you would do if you were printing out the number of vowels you found.
"Take the risk of thinking for yourself, much more happiness, truth, beauty, and wisdom will come to you that way" - Christopher Hitchens

#3 User is offline   Romeo29 

  • Learning To Bleep
  • PipPipPipPipPipPip
  • Find Topics
  • Group: BC Advisor
  • Posts: 2,814
  • Joined: 06-July 08
  • Gender:Not Telling
  • Location:127.0.0.1

Posted 26 July 2009 - 06:44 PM

You forgot to print the vowel in this line :

System.out.println("there is only one vowel in the string you entered and that is --------");

[url="http://www.avast.com/"]avast! free antivirus[/url]

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users