Reputation: 1
I want to write a java code for getting an output like this, but i am not able to do so, instead i get an output something like this "The number of vowels is: 11010" can some one plese tell me what am i doing wrong?
Input : Montreal
Output : The number of vowels is: 1 a, 1 e, 1 o
import java.util.Scanner;
public class Problem1
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str;
int cnt[] = new int[5];
str = sc.nextLine();
for (int i = 0; i < str.length(); ++i) {
if (str.charAt(i) == 'a' || str.charAt(i) == 'A') {
cnt[0]++;
}
if (str.charAt(i) == 'e' || str.charAt(i) == 'E') {
cnt[1]++;
}
if (str.charAt(i) == 'i' || str.charAt(i) == 'I') {
cnt[2]++;
}
if (str.charAt(i) == 'o' || str.charAt(i) == 'O') {
cnt[3]++;
}
if (str.charAt(i) == 'u' || str.charAt(i) == 'U') {
cnt[4]++;
}
}
System.out.println("The number of vowels is: " + cnt[0] + cnt[1] + cnt[2] + cnt[3] + cnt[4]);
}
}
Upvotes: 0
Views: 128
Reputation: 100
You forgot to print the vowel before its count
import java.util.Scanner;
public class Problem1
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str;
int cnt[] = new int[5];
str = sc.nextLine();
char c;
for (int i = 0; i < str.length(); ++i) {
c = str.charAt(i);
if (c == 'a' || c == 'A') {
cnt[0]++;
}
else if (c == 'e' || c == 'E') {
cnt[1]++;
}
else if (c == 'i' || c == 'I') {
cnt[2]++;
}
else if (c == 'o' || c == 'O') {
cnt[3]++;
}
else if (c == 'u' || c == 'U') {
cnt[4]++;
}
}
boolean b = false;
StringBuilder sb = new StringBuilder();
sb.append("The number of vowels is:");
if (cnt[0]!=0){
sb.append(cnt[0]);
sb.append(" a");
b = true;
}
if (cnt[1]!=0){
if (b) {sb.append(",");}
sb.append(cnt[1]);
sb.append(" e");
b = true;
}
if (cnt[2]!=0){
if (b) {sb.append(",");}
sb.append(cnt[2]);
sb.append(" i");
b = true;
}
if (cnt[3]!=0){
if (b) {sb.append(",");}
sb.append(cnt[3]);
sb.append(" o");
b = true;
}
if (cnt[4]!=0){
if (b) {sb.append(",");}
sb.append(cnt[4]);
sb.append(" u");
b = true;
}
System.out.println(sb.toString());
}
}
Upvotes: 0
Reputation: 521053
Just change your print statement to also print the vowel after printing its count:
StringBuilder output = new StringBuilder("The number of vowels is: "+ cnt[0] + " a, " + cnt[1] + " e, " + cnt[2] + " i, " + cnt[3] + " o, " + cnt[4] + " u";
System.out.println(output);
By the way, you could also use a map to store the vowels and counts, instead of an array:
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
Map<Character, Integer> map = new HashMap<>();
for (int i=0; i < str.length(); ++i) {
char letter = Character.toLowerCase(str.charAt(i));
if (letter == 'a' || letter == 'e' || letter == 'i' ||
letter == 'o' || letter == 'u') {
Integer count = map.get(letter);
map.put(letter, count == null ? 1 : count + 1);
}
}
StringBuilder output = new StringBuilder();
for (Map.Entry<Character, Integer> entry : map.entrySet()) {
if (output.length() > 0) output.append(", ");
output.append(entry.getValue() + " " + entry.getKey());
}
System.out.println("The number of vowels is: " + output.toString());
This prints:
The number of vowels is: 1 a, 1 e, 1 o
Upvotes: 1