Reputation: 311
I have this problem for homework (I'm being honest, not trying to hide it at least) And I'm having problems figuring out how to do it.
Given the following declarations : String phrase = " WazzUp ? - Who's On FIRST ??? - IDUNNO"; Write the necessary code to count the number of vowels in the string and print appropriate message to the screen.
Here's the code I have so far:
String phrase = " WazzUp ? - Who's On FIRST ??? - IDUNNO";
int i, length, vowels = 0;
String j;
length = phrase.length();
for (i = 0; i < length; i++)
{
j = phrase.substring(i, i++);
System.out.println(j);
if (j.equalsIgnoreCase("a") == true)
vowels++;
else if (j.equalsIgnoreCase("e") == true)
vowels++;
else if (j.equalsIgnoreCase("i") == true)
vowels++;
else if (j.equalsIgnoreCase("o") == true)
vowels++;
else if (j.equalsIgnoreCase("u") == true)
vowels++;
}
System.out.println("Number of vowels: " + vowels);
However, when I run it it just makes a bunch of blank lines. Can anyone help?
Upvotes: 9
Views: 74727
Reputation: 378
I know I'm a little late but maybe this response could help other people in the future if they're working with accents
To avoid problems we can normalize the input text to NFD with Normalizer which separates the accents (be: umlauts, grave accent, acute accent, circumflex accent, etc.) in letter and accent.
Example: "schön" ----NFD----> "scho\u0308n"
The solution is
Normalizer
and replace the accents with (replaceAll
)import java.text.Normalizer;
public class Main {
private static String str = "This ïs a téxt with vòwêls";
public static void main(String[] args) {
// Normalizing the text
// Replacing all the accents
// Convert to lowercase
String text = Normalizer.normalize(str, Normalizer.Form.NFD)
.replaceAll("[\\u0300-\\u036f]", "")
.toLowerCase();
// We replace everything but vowels
int vowels_count = text.replaceAll("[^aeiou]", "").length();
System.out.printf("%s%s%s %d %s",
"\nThe text \"",
str,
"\" has",
vowels_count,
"vowels"
);
}
}
Upvotes: 0
Reputation: 10059
To Count number of Vowels in a String:
class Test {
static int a;
public static String countVowel(String strName) {
char ch[] = strName.toCharArray();
for(int i=0; i<ch.length; i++) {
switch(ch[i]) {
case 'a':
a++;
break;
case 'e':
a++;
break;
case 'i':
a++;
break;
case 'o':
a++;
break;
case 'u':
a++;
break;
}
}
strName = String.valueOf(a);
return strName;
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print(countVowel(s.nextLine()));
}
}
Upvotes: 0
Reputation: 101
I know that's an old edit , but i think it's better if you use and array for the vowels :
public static void main(String[] args) {
String phrase = " WazzUp ? - Who's On FIRST ??? - IDUNNO".toLowerCase();
int vowels = 0;
String[] array = {"a", "e", "i", "o", "u"};
for (int i = 0; i < phrase.length(); i++) {
String j = phrase.substring(i, i + 1);
System.out.println(j);
for (int n = 0; n < array.length; n++) {
if (j.equals(array[n])) {
vowels++;
}
}
}
System.out.println("Number of vowels: " + vowels);
}
Upvotes: 0
Reputation: 1
Using Collections:
String word = "busiunesuse";
char[] wordchar= word.toCharArray();
Character allvowes[] ={'a','e','i','o','u'};
Map<Character, Integer> mymap = new HashMap();
for(Character ch: wordchar)
{
for(Character vowels : allvowes)
{
if(vowels.equals(ch))
{
if(mymap.get(ch)== null)
{
mymap.put(ch, 1);
}
else
{
int val = mymap.get(ch);
mymap.put(ch, ++val);
}
}
}
}
Set <Character> myset = mymap.keySet();
Iterator myitr = myset.iterator();
while(myitr.hasNext())
{
Character key = (Character) myitr.next();
int value = mymap.get(key);
System.out.println("word:"+key+"repetiotions:"+value);
}
}
}
Upvotes: 0
Reputation: 13565
I don't see the need to have a print statement in the loop.
String s = "Whatever you want it to be.".toLowerCase();
int vowelCount = 0;
for (int i = 0, i < s.length(); ++i) {
switch(s.charAt(i)) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
vowelCount++;
break;
default:
// do nothing
}
}
This converts the string to lowercase, and checks all the characters in the string for vowels.
Upvotes: 9
Reputation: 1242
Improving on an above answer to consider vowels in caps:
System.out.println(s.length() - s.toLowerCase().replaceAll("a|e|i|o|u|", "").length());
Upvotes: 4
Reputation: 28519
This could/should be a one-liner
System.out.println("the count of all vowels: " + (phrase.length() - phrase.replaceAll("a|e|i|o|u", "").length()));
and its String only methods
Upvotes: 8
Reputation: 1164
public class JavaApplication2 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
System.out.println("Enter some text");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = br.readLine().toLowerCase();
char[] vowel = new char[]{'a', 'e', 'i', 'o', 'u'};
int[] countVowel = new int[5];
for (int j = 0; j < input.length(); j++) {
char c =input.charAt(j);
if(c=='a')
countVowel[0]++;
else if(c=='e')
countVowel[1]++;
else if(c=='i')
countVowel[2]++;
else if(c=='o')
countVowel[3]++;
else if(c=='u')
countVowel[4]++;
}
for (int i = 0; i <countVowel.length; i++) {
System.out.println("Count of vowel " + vowel[i] + "=" + countVowel[i]);
}
}
}
Upvotes: -1
Reputation: 4111
phrase.substring(i, i++);
should be phrase.substring(i, i + 1);
.
i++
gives the value of i
and then adds 1 to it. As you have it right now, String j
is effectively phrase.substring(i, i);
, which is always the empty string.
You don't need to change the value of i
in the body of the for
loop since it is already incremented in for (i = 0; i < length; i++)
.
Upvotes: 9
Reputation: 1647
The i++ increments i after is has been used, so you're essentially saying string.substring(i,i). Since the ending mark is exclusive, this will always return an empty string. An easy fix would be to just change it to
j = phrase.substring(i, ++i);
Hope that helps!
Upvotes: 0