1

What this programme does is;

Prompts for number of testcases,
User then inputs lines of strings which are the test case.
Program is to count the number of vowels in each test case. then print out the each test case.

btw for this particular program "y" is also a vowel For instance; Number of test:

4
githsajklu
bsa uiqsacva
o h qi samsauq sajahhsa
skajayyosak

answer:
5 4 13 2

The problem is that the program doesnt read the last line/input. it just brings the count for the first 3 inputs but not the last. I hope I am clear enough

     import java.util.Scanner;
     /*
      * Counts number of Vowels in each line
      */
      public class VowelCount {
     /*
     * 
     */

   public static void main(String[] args){  

    Scanner input = new Scanner(System.in);  
    Scanner input2 = new Scanner(System.in);  

    //prompt user for number of test case     
     System.out.println("Type in number of Test Cases");  
    int testcases = input.nextInt();  

     String[] line = new String[testcases];  
     String newline;  

    for(int i=0; i<testcases; i++)  
    {  
    //initialize input to line 1,2,3 e.t.c  
     line[i] = input2.nextLine();  

     //remove all white spaces   
     newline =line[i].replaceAll(" ", "");  

     display(testcases, newline);  
    }  


}  
/*  
 * counts how many vowels are in eace line of input
 */  

public static int Counter(String input)  
{
    //start count from 0;  
    int counter = 0;  
    for(int i = 0; i<input.length(); i++)  
    {
        //set character at position i to Dstr  
    char dStr = input.charAt(i);  

    //compare if dstr is a vowel  
    if(dStr == 'i' || dStr == 'u' || dStr == 'o' || dStr == 'a' || dStr   == 'e' || dStr == 'y')  
    {
        //increase when characte is a vowel  
        counter++;  
    }
    }
    //return the last count  
    return counter;  
}

/*
 * diplay the total count;
 */
public static void display(int testcases, String input)
{

    System.out.print(" "+Counter(input));
}

}

bright
  • 131
  • 9

3 Answers3

0

Do a scan.nextLine() after you read the amount of test cases. Don't know why it works like that, someone feel free to explain, but if you're reading a an int, then a string, you can either do

int n = Integer.parseInt(scan.nextLine());

or

int n = scan.nextInt();
scan.nextLine();

Also, I know you didn't ask, but here's a much simpler way to count the amount of vowels.

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int n = scan.nextInt();
    scan.nextLine();
    for(int i = 0; i < n; i++){
        String str = scan.nextLine();
        System.out.println(str.replaceAll("[^AEIOUaeiouYy]", "").length());
    }
}

What that does is erase everything that is not a vowel (and y), and prints the length of it. Really not sure if it's faster, but it's a lot simpler.

Danny0317
  • 31
  • 3
0

Actually, here I'm testing and it is working just fine, it is getting exactly how many inputs I asked for.

My console:

Type in number of Test Cases
4
werty
 2
sdfghj
 0
xdcfvgh
 0
xsdfgh
 0
MyName
  • 69
  • 7
0

Sometimes, having less code can make things clearer:

void printVowelCount(String text) {
    System.out.println(text.replaceAll("[^aeiouAEIOU]", "").length());
}
Bohemian
  • 365,064
  • 84
  • 522
  • 658