Andrew Sanders Andrew Sanders - 2 months ago 7
Java Question

Finding and counting if there are the correct number of vowels in a sentence

I've been looking around the site and can't find an answer to my question. I need to be able to look through a given amount of strings and count the amount of vowels in the word. If the amount of vowels match the numbers entered before, then print YES. If one word doesn't have the right amount of vowels, print NO. I tried using a String array then going through each word in the array to look for the vowels, increment the vowel check (vocheck) for every vowel it finds, then check if vocheck equals the appropriate number. But, when I check to see if the vocheck for loop works, it just keeps vocheck as 0. What's wrong?

import java.util.*;
public class verses {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int lines = input.nextInt();
int[] arr = new int[lines];
int vowels = 0;
boolean vcheck = true;
int vocheck = 0;
for(int i = 0; i < lines; i++){
arr[i] = input.nextInt();
}
String[] word = new String[lines];
for(int x = 0; x < word.length; x++){
word[x] = input.nextLine();
}
word[word.length-1] = input.nextLine();

for(int m = 0; m < lines; m++){
for(int y = 0; y < word[y].length(); y++){
if(word[m].charAt(y) == 'a'
|| word[m].equals('e')
|| word[m].equals('i')
|| word[m].equals('o')
|| word[m].equals('u')
|| word[m].equals('y')){
vocheck++;
}
}
if(vocheck != arr[m]){
vcheck = false;
}
}

if(vcheck == true){
System.out.println("YES");
}else{
System.out.println("NO");
}

}



}


This is what a sample input would look like
3
2 2 3
intel
code
challenge

This should print YES because there are three words, the first has 2 vowels, the second has 2 vowels, and the third has 3 vowels

Answer

There are multiple problems here:

  • Add input.nextLine(); before reading words. This is necessary otherwise first entry in array word would be rest of the part of the previous line.
  • word[word.length-1] = input.nextLine(); is not necessary
  • vocheck must be initialized for each iteration of the loop
  • for(int y = 0; y < word[y].length(); y++){ should be for(int y = 0; y < word[m].length(); y++){. Notice y is changed to m.
  • Also other conditions are not consistent. They shouldn't be like word[m].equals('e') but like word[m].charAt(y) == 'e'

    import java.util.*;public class verses {
    
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int lines = input.nextInt();
        int[] arr = new int[lines];
        int vowels = 0;
        boolean vcheck = true;
        int vocheck = 0;
        for(int i = 0; i < lines; i++){
            arr[i] = input.nextInt();
        }
        input.nextLine();
        String[] word = new String[lines];
        for(int x = 0; x < word.length; x++){
            word[x] = input.nextLine();
        }
        //word[word.length-1] = input.nextLine();
    
        for(int m = 0; m < lines; m++){
            vocheck = 0;
            for(int y = 0; y < word[m].length(); y++){
                if(word[m].charAt(y) == 'a'
                        || word[m].charAt(y) == 'e'
                        || word[m].charAt(y) == 'i'
                        || word[m].charAt(y) == 'o'
                        || word[m].charAt(y) == 'u'
                        || word[m].charAt(y) == 'y'){
                    vocheck++;
                }
            }
            if(vocheck != arr[m]){
                vcheck = false;
            }
    
        }
        if(vcheck == true){
            System.out.println("YES");
        }else{
            System.out.println("NO");
        }
    }
    

    }