Reputation: 33
This is my code here, I am trying to find how many vowels are there in the string vowelCount and keepng track in a int count[]; so the string is ("black banana republic boots") and the output should be {4, 1, 1, 2, 1}. Is my math correct. If not whats wrong
class lab204 {
public static String vowelCount() {
String vowelCount[] = ("black banana republic boots");
int count[];
int counter = 0;
for( int i=0; i<s.length(); i++ ) {
if( vowelCount.charAt(i) == 'a' ) {
counter++;
}
if( vowelCount.charAt(i) == 'e' ) {
counter++;
}
if( vowelCount.charAt(i) == 'i' ) {
counter++;
}
if( vowelCount.charAt(i) == 'o' ) {
counter++;
}
if( vowelCount.charAt(i) == 'u' ) {
counter++;
}
}
}
}
Upvotes: 0
Views: 286
Reputation: 4006
First of all, your code won't compile. This is because you are creating an array incorrectly. Note that you don't need an array, unless you want to count multiple strings at once.
public static String vowelCount() {
//Does not need to be an array
String sentence = "black banana republic boots";
//Create your array to count 5 vowels
int[] vowelCount = { 0, 0, 0, 0, 0 };
for( int i = 0; i < sentence.length(); i++ ) {
//Don't forget to check for uppercase too!
if( sentence.charAt(i) == 'a' || sentence.charAt(i) == 'A' ) {
vowelCount[0]++;
}
if( sentence.charAt(i) == 'e' || sentence.charAt(i) == 'E' ) {
vowelCount[1]++;
}
if( sentence.charAt(i) == 'i' || sentence.charAt(i) == 'I' ) {
vowelCount[2]++;
}
if( sentence.charAt(i) == 'o' || sentence.charAt(i) == 'O' ) {
vowelCount[3]++;
}
if( sentence.charAt(i) == 'u' || sentence.charAt(i) == 'U' ) {
vowelCount[4]++;
}
}
}
I'm assuming you don't know much about arrays, so I suggest reading up on them here.
Also, your function is declared to return a string, when it seems like you really want to return the array with the count of the vowels. Change your function declaration like so:
public static int[] vowelCount() {
And at the end of your function, after the for loop, return the vowelCount
array:
public static int[] vowelCount() {
String sentence = "black banana republic boots";
int[] vowelCount = { 0, 0, 0, 0, 0 };
for (int i = 0; i < sentence.length(); i++) {
//If statements
//...
} //End of for loop
//Return the array counting the vowels
return vowelCount;
} //End of function
Upvotes: 2
Reputation: 7255
Some points:
1)If else when it has one line it doen't need {} brackets
2)Here is a sample code for your situation
public class VowelTester {
char[] vowels = {'a','e','i','o','u'};
/** Check for vowels
* @param sample
* @return array containing the counter of each vowel [a,e,i,o,u]
*/
public int[] checkForVowels(String sample) {
int[] array = new int[5];
for (int i = 0; i < sample.length(); i++) {
if (sample.charAt(i) == 'a')
array[0] += 1;
if (sample.charAt(i) == 'e')
array[1] += 1;
if (sample.charAt(i) == 'i')
array[2] += 1;
if (sample.charAt(i) == 'o')
array[3] += 1;
if (sample.charAt(i) == 'u')
array[4] += 1;
}
System.out.println("a:"+array[0]+" e:"+array[1]+" i:"+array[2]+" o:"+array[3]+" u:"+array[4]);
return array;
}
public static void main(String[] args){
new VowelTester().checkForVowels("Find out how many vowels i have");
}
}
Upvotes: -1
Reputation: 798
please fill in the blanks
public static String vowelCount() {
String s = ("black banana republic boots"); //test string
int count[] = new int[__]; //to use an int array for counting
for( int i=0; i<s.length(); i++ ) { //looking at test string, one char at a time
if( s.charAt(i) == 'a' ) {
count[0]++; //increment the first array element
}
if( s.charAt(i) == 'e' ) {
___________ //increment the second array element
}
if( s.charAt(i) == 'i' ) {
___________ //...
}
if( s.charAt(i) == 'o' ) {
__________ //...
}
if( s.charAt(i) == 'u' ) {
_____________
}
}
}
Upvotes: 0