Home > Blockchain >  Need to print a string array
Need to print a string array

Time:12-29

My task is to read the strings by input, and then display the strings that have more than 4 vowels in each. I have this code:


import java.util.Scanner;
public class Main {
    static boolean vowelChecker(char a) {
        a = Character.toLowerCase(a);
        return (a=='a' || a=='e' || a=='i' || a=='o' || a=='u' || a=='y');
    }
    static int counter(String str) {
        int count = 0;
        for (int i = 0; i < str.length(); i  )
            if (vowelChecker(str.charAt(i))) {
                  count;
            }
        return count;
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the number of elements you want to store: ");
        int n;
        n=scanner.nextInt();
        String[] array = new String[100];
        System.out.println("Enter the elements of the array: ");
        for(int i=0; i<n; i  )
        {
            array[i]=scanner.nextLine();
        }
        String str = scanner.nextLine();
        int b = counter(str);
        if (b > 4) {
            System.out.println("What do I write here?");
        }
    }
}

And my question is: how to correctly write the code so that the output would be strings from input that have more than 4 vowels?

CodePudding user response:

import java.util.Scanner;

public class Test {
    private static final char[] VOWELS = new char[]{'a', 'e', 'i', 'o', 'u', 'y'};

    public static void main(String[] args) {
        // Initialize and open a new Scanner
        final Scanner scanner = new Scanner(System.in);

        // Get the number of lines we want to analyze
        System.out.print("Enter the number of elements you want to store: ");
        final int n = Integer.parseInt(scanner.nextLine());

        // Get all the lines from the user
        System.out.println("Enter the elements of the array: ");
        final String[] lines = new String[n];
        for(int i = 0; i < n; i  ) {
            lines[i]=scanner.nextLine();
        }

        // Close the Scanner
        scanner.close();

        // Check each line, count the number of vowels, and print the line if it has more than 4 vowels.
        System.out.println("\nInputs that have more than 4 vowels");
        for(int i = 0; i < n; i  ) {
            if (countVowels(lines[i]) > 4) {
                System.out.println(lines[i]);
            }
        }
    }

    private static boolean isVowel(char a) {
        for (int i = 0; i < VOWELS.length; i  ) {
            if (Character.toLowerCase(a) == VOWELS[i]) {
                return true;
            }
        }
        return false;
    }

    private static int countVowels(final String str) {
        int count = 0;
        for (int i = 0; i < str.length(); i  ) {
            if (isVowel(str.charAt(i))) {
                count  ;
            }
        }
        return count;
    }
}

CodePudding user response:

Like others have pointed out, you never read from the array. Read for each of the strings, if the counter() returns a value larger than 4, we want to print it. So, this could do the trick:

for(int i=0; i<n; i  )
{
  if (counter(array[i]) > 4)
    System.out.println(array[i]);
}

Using nextInt won't absorb the newline character \n, that's why you are inputting 1 string less. There are some workarounds that you can read about here.

CodePudding user response:

So this first part makes sense :

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter the number of elements you want to store: 
    ");
    int n;
    n=scanner.nextInt();
    String[] array = new String[100];
    System.out.println("Enter the elements of the array: ");
    for(int i=0; i<n; i  )
    {
        array[i]=scanner.nextLine();
    }

after this part I would just do :

for (String s: array) {
if (Objects.isNull(s))
    break;
 if (count(s) >= 5) {
System.out.println(s);
}
}

CodePudding user response:

import java.util.Arrays;
import java.util.Objects;
import java.util.Scanner;

class Main {

    static long counter(String str) {

        return Arrays.stream(str.split(""))
                .filter(c -> Arrays.asList("a", "e", "i", "o", "u", "y").contains(c.toLowerCase()))
                .count();
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the number of elements you want to store: ");
        int n;
        n = scanner.nextInt();
        scanner.nextLine();

        String[] array = new String[n];

        System.out.println("Enter the elements of the array: ");

        for (int i = 0; i < n; i  ) {
            String str = scanner.nextLine();

            if (counter(str) > 4) {
                array[i] = str;
            }
        }

        System.out.println(Arrays.toString(Arrays.stream(array).filter(Objects::nonNull).toArray(String[]::new)));
    }
}
  •  Tags:  
  • java
  • Related