0

I am writing a program in Java that is supposed to give an output like this:

  • vowels = 8
  • upper = 2
  • digits = 5
  • whitespace = 6
  • vowel i occurs the most = 4

My code compiles and I was successful with everything except determining which vowel occurs the most.

I am not sure what I should do, first count how many times an individual vowel (like just "a") occurs (as opposed to how many total vowels occur in the string). After I find the total of each individual vowels, I am not sure what to use to determine the vowel with the maximum value. Once I am able to get these two steps, I then am not exactly sure how to properly output. I would prefer to accomplish this with an if statement however, I don't know if that is possible or not.

Any help/tips will be greatly appreciated, here is the code I have written:

    // which vowel occurs the most
    if (ch == 'a')
      vowelA++;
    else if (ch == 'e')
      vowelE++;
    else if (ch == 'i')
      vowelI++;
    else if (ch == 'o')
      vowelO++;
    else if (ch == 'u')
      vowelU++;

    if (vowelA > vowelE && vowelA > vowelI && vowelA > vowelO && vowelA > vowelU)
    {
      maxVowels = vowelA;
    }
  }

// OUTPUT
System.out.println("vowel" + " " + "occurs the most = " + maxVowels);

 }
}
agung
  • 3
  • 3

3 Answers3

1

There can be many ways. I am writing one of them. You can try it:

// for number of VOWELS
for (int i = 0; i < str.length(); i++)
{
    ch = str.charAt(i);
    ch = Character.toLowerCase(ch);

    // is this a vowel
    if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
    {
        vowels++;
    }

    // which vowel occurs the most
    if (ch == 'a')
        vowelA++;
    else if (ch == 'e')
        vowelE++;
    else if (ch == 'i')
        vowelI++;
    else if (ch == 'o')
        vowelO++;
    else if (ch == 'u')
        vowelU++;

}

maxVowels = Math.max(vowelA, Math.max(vowelE, Math.max(vowelI, Math.max(vowelO, vowelU))));

Output:

enter image description here

Note: I have just added maxVowels = Math.max(vowelA, Math.max(vowelE, Math.max(vowelI, Math.max(vowelO, vowelU)))); before upper case logic and removed if-condition where you are storing maxVowels value.

Another Way: Replace maxVowels = Math.max(vowelA, Math.max(vowelE, Math.max(vowelI, Math.max(vowelO, vowelU)))); with Collections.max(Arrays.asList(vowelA, vowelE, vowelI, vowelO, vowelU));

Nitin Bisht
  • 5,053
  • 4
  • 14
  • 26
  • Thank you so much!! that helped tremendously. Now all i have to figure out is how to properly write my System.out.println statement – agung Mar 24 '20 at 18:01
  • 1
    Instead of chained `Math.max` calls, you can just do `Collections.max(Arrays.asList(vowelA, vowelE, vowelI, vowelO, vowelU))`. – Fureeish Mar 24 '20 at 20:39
  • @Fureeish you are right. I have already mentioned there can be many ways. It is also one of them. – Nitin Bisht Mar 25 '20 at 03:46
0

Try this code:

{
   public static void main (String []args)
 {
    Scanner scan = new Scanner(System.in);
    String str = scan.nextLine();

    int vowels = 0, digits = 0, spaces = 0, upper = 0;
    String line = str;
    int max_vowel_count = 0;
    for(int i = 0; i < line.length(); ++i)
    {
        char ch = line.charAt(i);
        if(ch == 'a' || ch == 'e' || ch == 'i'
            || ch == 'o' || ch == 'u') {


            int a_count = line.length() - line.replace("a", "").length();
            int e_count = line.length() - line.replace("e", "").length();
            int i_count = line.length() - line.replace("i", "").length();
            int o_count = line.length() - line.replace("o", "").length();
            int u_count = line.length() - line.replace("u", "").length();

            max_vowel_count = Math.max(a_count, Math.max(e_count, Math.max(i_count, Math.max(o_count, u_count))));

            ++vowels;
        }
        else if( ch >= '0' && ch <= '9')
        {
            ++digits;
        }
        else if (ch >= 'A' && ch <= 'Z'){ 
            ++upper; 
        }
        else if (ch ==' ')
        {
            ++spaces;
        }
    }

    System.out.println("Vowels: " + vowels);
    System.out.println("Digits: " + digits);
    System.out.println("Upper Case: " + upper);
    System.out.println("White spaces: " + spaces);
    System.out.println("Max Vowel Count "+ max_vowel_count);

}
Abhishek Kulkarni
  • 1,747
  • 1
  • 6
  • 8
0

If you are interested in a stream based solution:


    public static void main(String[] args) {

        String input = "This String has vowels and 12345 digits";

        Map<String, Integer> counts = input.chars()
                .mapToObj((str) -> charTypes(str))
                .flatMap((it) -> it)
                .collect(Collectors.toMap(Function.identity(), v -> 1, Integer::sum));

        System.out.println("Vowels: " + counts.getOrDefault("vowel", 0));
        System.out.println("Upper Case: " + counts.getOrDefault("upper", 0));
        System.out.println("Digits: " + counts.getOrDefault("digit", 0));
        System.out.println("White spaces: " + counts.getOrDefault("whitespace", 0));

        Optional<Map.Entry<String, Integer>> maxVowels = counts.entrySet()
                .stream()
                .filter((str) -> str.getKey().length() == 1)
                .filter((str) -> vowels.contains((int)str.getKey().charAt(0)))
                .max(Comparator.comparingInt(Map.Entry::getValue));

        if (maxVowels.isPresent()) {
            System.out.println("Max Vowel was " + maxVowels.get().getKey() + " with  " + maxVowels.get().getValue() + " occurrences");
        } else {
            System.out.println("No vowels found");
        }
    }

    private static Set<Integer> vowels = Set.of((int) 'a', (int) 'e', (int) 'i', (int) 'o', (int) 'u');

    public static boolean isVowel(int c) {
        return vowels.contains(Character.toLowerCase(c));
    }

    public static Stream<String> charTypes(int c) {
        List<String> types = new ArrayList<>();

        if (isVowel(c)){
            types.add("vowel");
            types.add(String.valueOf((char)c));
        }

        if (Character.isWhitespace(c)) {
            types.add("whitespace");
        }

        if (Character.isDigit(c)) {
            types.add("digit");
        }

        if (Character.isUpperCase(c)) {
            types.add("upper");
        }

        return types.stream();
    }

(Making vowel type an enum would be an improvement)

Jindra
  • 780
  • 13
  • 39
  • Error "The method of(int, int, int, int, int) is undefined for the type Set" on private static Set vowels = Set.of((int) 'a', (int) 'e', (int) 'i', (int) 'o', (int) 'u'); line. – Nitin Bisht Mar 25 '20 at 04:25
  • Set.of was added in Java 9. If you use older versions, you need to create a ``HashSet`` object and then ``add()`` all the elements individually. – Jindra Mar 25 '20 at 09:42