Home > Enterprise >  Number of vowels from string array
Number of vowels from string array

Time:10-18

The way I find the number of vowels is wrong, but I don't know how to fix it can you help me? this is my program:

import java.util.Scanner;

public class Array2 {

public static void main(String[] args) {

  Scanner keyboard=new Scanner(System.in);   
  int vowels = 0, length = 0;
  
  String[] values = new String[5]; 
  
  for (int i = 0; i<values.length; i  )
  {
     System.out.print("Enter the string:");
     values[i] = keyboard.nextLine();
     
        if(values[i].charAt(i) == 'a' ||values[i].charAt(i) == 'e' || values[i].charAt(i) == 'i' || values[i].charAt(i) == 'o' || values[i].charAt(i) == 'u')
     {
        vowels  ;
     }
     
  }


  System.out.printf("s s s s\n","Number","Value", " Length", "        Number of Vowels"); 
  int[] number = new int[5]; 
  for(int n=0; n<number.length;n  )
  {  
  

     System.out.printf("s s s s\n", n 1,values[n],values[n].length(),vowels ); 
     System.out.println();  
  }  

} }

output:

      Number         Value            Length         Number of Vowels
          1      wonderland              10               1

          2         program               7               1

          3          school               6               1

          4           hello               5               1

          5           mouse               5               1

I don't know why my number of vowels is wrong.

CodePudding user response:

You can define a function to count the number of vowels as shown below and then use the same in your loop:

int vowelsCount(String str) {
    int vowels = 0;
    for (int i = 0; i < str.length(); i  )
        if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o'
                || str.charAt(i) == 'u')
            vowels  ;

    return vowels;
}

Full Demo:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        String[] values = new String[5];

        for (int i = 0; i < values.length; i  ) {
            System.out.print("Enter the string:");
            values[i] = keyboard.nextLine();
        }

        System.out.printf("s s s s\n", "Number", "Value", " Length", "        Number of Vowels");
        for (int n = 0; n < values.length; n  ) {
            System.out.printf("s s s s%n", n   1, values[n], values[n].length(), vowelsCount(values[n]));
        }
    }

    static int vowelsCount(String str) {
        int vowels = 0;
        for (int i = 0; i < str.length(); i  )
            if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o'
                    || str.charAt(i) == 'u')
                vowels  ;

        return vowels;
    }
}

Output:

 Number           Value          Length         Number of Vowels
      1      wonderland              10               3
      2         program               7               2
      3          school               6               2
      4           hello               5               2
      5           mouse               5               3

CodePudding user response:

Using Java 8

You can try this approach using java 8 concepts as shown below:

Approach Here:

I have converted the input array into list using Arrays.asList and then converted each input string into char stream and find the count of vowels in each string.

public class Test {

    public static void main(String[] args) {

        Scanner keyboard = new Scanner(System.in);
        String[] values = new String[5];

        for (int i = 0; i < values.length; i  ) {
            System.out.print("Enter the string:");
            values[i] = keyboard.nextLine();
        }
        AtomicInteger counter = new AtomicInteger(1);
        System.out.printf("s s s s\n", "Number", "Value", " Length", "        Number of Vowels");
        Arrays.asList(values).forEach(x -> {
            long count = x.chars().filter(ch -> (ch == 'a' || ch == 'e' ||
                    ch == 'i' || ch == 'o' || ch == 'u')).count();
        System.out.printf("s s s s\n", counter.getAndIncrement(), x, x.length(), count);
        });
    }
}

Output:

Enter the string:wonderland
Enter the string:program
Enter the string:school
Enter the string:hello
Enter the string:mouse

         Number           Value          Length         Number of Vowels
              1      wonderland              10               3
              2         program               7               2
              3          school               6               2
              4           hello               5               2
              5           mouse               5               3

CodePudding user response:

    public static void main(String[] args) {
        Scanner keyboard=new Scanner(System.in);
        int vowels = 0, length = 0;
        String[] values = new String[5];
        for (int i = 0; i<values.length; i  )
        {
            System.out.print("Enter the string:");
            values[i] = keyboard.next();
        }
        System.out.printf("s s s s\n","Number","Value", " Length", "        Number of Vowels");
        int[] number = new int[5];
        for(int n=0; n<number.length;n  ) {
            vowels = values[n].length() - values[n].replaceAll("[aeiouAEIOU]", "").length();
            System.out.printf("s s s s\n", n 1,values[n],values[n].length(),vowels );
            System.out.println();
        }
    }

I compare the length of the String with the length of the String without vowels.

replaceAll() expects a regular expression as first argument. Because I leave the second argument empty, all vowels will be removed.

Output:

         Number           Value          Length         Number of Vowels
              1           Hello               5               2
              2              My               2               0
              3            name               4               2
              4              is               2               1
              5       Bumblebee               9               4
  • Related