public class one
{
public static void main(String [] args)
{
}
public static int vowels( String s )
{
int countA = 0; int countE = 0; int countI = 0;
int countO = 0; int countU = 0;
for(int i = 0; i < s.length(); i++)
{
if(s.charAt(i) == 'a')
{
countA++;
}
else if(s.charAt(i) == 'e')
{
countE++;
}
else if(s.charAt(i) == 'i')
{
countI++;
}
else if(s.charAt(i) == 'o')
{
countO++;
}
else if(s.charAt(i) == 'u')
{
countU++;
}
}
return countA; return countE; return countI;
return countO; return countU;
}
}
The program I'am making will count the number of vowels in a string. I'am trying to get a way to apply it to main and output the result. I tried doing different returns like below but its not working..
You can only return a single value from a method call. However, that value can be an array or a collection.
Two options to consider:
The first approach is cleaner and more reusable (IMO) but less efficient. You'd have something like:
String text = ...;
System.out.println("The vowel a appears " + count(text, 'a') + " times.");
System.out.println("The vowel e appears " + count(text, 'e') + " times.");
System.out.println("The vowel i appears " + count(text, 'i') + " times.");
System.out.println("The vowel o appears " + count(text, 'o') + " times.");
System.out.println("The vowel u appears " + count(text, 'u') + " times.");
...
private static int count(String text, char charToCount) {
int count = 0;
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) == charToCount) {
count++;
}
}
return count;
}
See more on this question at Stackoverflow