0

As you can see, I have tried in my own unusual way but i am asking you, can you help me to know where i was wrong in the below code, please.

public class vow {
    static void myvow(String name) {
        int count = 0;
        String[] vowels = {"a", "e", "i", "o", "u"};
        
        int n = name.length();
        for (int i = 0; i == n; i++) {
            String check = name.substring(i, i + 1);
            for (String in : vowels) {
                if (in == check) {
                    count = count + 1;
                }
            }
        }
        System.out.println(count);
    }

    public static void main(String[] args) {
        myvow(" aeiou");
    }
}
Turing85
  • 18,217
  • 7
  • 33
  • 58
  • `if (in.equals(check))` is what you're looking for. – rzwitserloot Aug 20 '21 at 14:50
  • you probably mean `i < n` or `i != n` not `i == n` – ATP Aug 20 '21 at 14:50
  • Check out [this article](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) for tips on debugging your code. – Code-Apprentice Aug 20 '21 at 14:53
  • first issue is with your for loop condition, second issue is string comparison. In Java, in order to compare 2 strings you have to use either `equals()` or `equalsIgnoreCase()` method. – Saravanakrishnan Pk Aug 20 '21 at 14:53
  • You are comparing Strings, this should be done with `equals()`. That being said, vowels should not be `Strings` but `char`. Define the vowels like this : `char[] vowels = {'a', 'e', 'i', 'o', 'u'}` and compare with `name.charAt(i)`. With `char` instead of `String`, the comparison with `==` will work – Arnaud Denoyelle Aug 20 '21 at 14:54

0 Answers0