How to count how many vowels our String have?

215 Views Asked by At

I'm new in Java and I'm trying to solve a challenge. I have to write some words and to compare which one is longer, and how many vowels the longer one have. Also if you write "end", writing of words to end and to print something else, in our case You didn't wrote any word.

Output Example in Terminal (CMD):

Write a word, or write 'end' to end writing: test
Write a word, or write 'end' to end writing: tee
Write a word, or write 'end' to end writing: testing
Write a word, or write 'end' to end writing: end

Word testing is longest and it have 2 vowels.

Output Example if you don't write any word:

Write a word, or write 'end' to end writing:
Write a word, or write 'end' to end writing:
Write a word, or write 'end' to end writing: end

You didn't wrote any word.

Program should be coded using Scanner (Input), Switch Case and Do While. Strings should be compared using method equalsIgnoreCase().

I tried many times, and what I did is only writing and deleting code.

This is my code:

import java.util.Scanner;

public class VowelFinder {

public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

        String word = null;
        int num = 0;
        String max = null;
        final String SENTINEL = "end";

        System.out.println(" ");

        do {
            System.out.print("Write a word, or write `" + SENTINEL + "` to end writing: ");
            word = scan.nextLine();
            if(!word.equalsIgnoreCase(SENTINEL)) {

                int nr = countVowels(word);
                if (num <= nr) {
                    num = nr;
                    max = word;
                }
            }
        } while (!word.equalsIgnoreCase(SENTINEL));

        if (max != null) {
            System.out.println(" ");
            System.out.println("Word `" + max + "` is longest word, and countains " + num + " vowels.");
        }
        else {
            System.out.println(" ");
            System.out.println("You din't wrote any word !");
        }

}   


private static int countVowels(String word) {
    int counter = 0;
    int vowels = 0;

    while(counter < word.length()){
        char ch = word.charAt(counter++);


        switch (ch) {
            //Lower Case
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'u':
            case 'y':
            //Upper Case
            case 'A':
            case 'E':
            case 'I':
            case 'O':
            case 'U':
            case 'Y':
            vowels++;

            default:
            // do nothing
        }
    }
    return vowels;
}

}

Problem is:

When I do so in terminal (CMD)

Write a word, or write 'end' to end writing:
Write a word, or write 'end' to end writing:
Write a word, or write 'end' to end writing: end

It prints me Word ' ' is longest word, and countains 0 vowels., but it should print You didn't wrote any word.

Can someone help me ? Where I did wrong ?

It should print me You didn't wrote any word if I don't write any word.

I hope I was clear and you can help me. If I wasn't clear please ask me.

Thanks for your contribution.

3

There are 3 best solutions below

5
On BEST ANSWER

I have made some changes and it worked for me...

if (max != null && !max.trim().isEmpty() && max.length()>0) {
            System.out.println(" ");
            System.out.println("Word `" + max + "` is longest word, and countains " + num + " vowels.");
        }
0
On

change the if condition to

if (word != null && !"".equals(word.trim()) && !word.equalsIgnoreCase(SENTINEL))

I added a null check and did a trim to remove the white spaces.

0
On

You could check in countVowels() whether the current word is nothing.

private static int countVowels(String word) {
    int counter = 0;
    int vowels = 0;
    if(word.length() == 0){
        return  -1;
    }
    ...
}

The return value is less than 0 so max won't be replaced.