-1

I'm doing this problem as practice for using methods outside of the main method. The problem needed us to make three different methods, each of which does a different task, but none of them have to relate with each other.

smallestNumber(): take 3 numbers inputted by the user and output the smallest number

average(): take 3 numbers inputted by the user and output the average

countVowels(): take a phrase inputted by the user and output the number of vowels in that phrase

For me, I am able to return a value from method 1 and method 2 back to the main method. For method 3, when I try to return the counter value, it ALWAYS returns 0 even if there ARE vowels in the phrase.

Can someone please explain what I am doing wrong? (sorry with indenting issues, I've never used Stack Overflow before)

I don't know why it keeps returning 0

public static int countVowels(String words) {
    int count=0;
    for (int i=0; i<words.length(); i++) {
        if (words.charAt(i) == 'a' || words.charAt(i) == 'e' || words.charAt(i) == 'i' || words.charAt(i) == 'o' || words.charAt(i) == 'u') {
            count++;
        } 
    }
    return(count);

}

Artvmvs
  • 9
  • 2
  • 3
    Density isn't a problem. Not indenting, however... That being said, if you declare your method to return a `String`, you can't then return an `int` and expect the compiler not to complain. – azurefrog May 14 '19 at 21:24
  • 2
    1) What is `count+=0;` supposed to do? It doesn't do anything. --- 2) You're missing the `}` of the `for` loop. If you **indented** the code correctly, you'd immediately see that. --- 3) `count` is an `int`, but the return type is a `String`. Why? Change return type to `int`. --- What is your method supposed to do? The name `method3` doesn't give any clue. You sure you want the test to be `words.indexOf("a")==i` and not `words.charAt(i) == 'a'`? – Andreas May 14 '19 at 21:28
  • You should read Eric Lippert's "How to debug small programs" to help yourself as a real programmer: https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – cellepo May 14 '19 at 23:47

3 Answers3

0

Your count is an int (not a String) so you should return an int. Instead of String.indexOf(String) you should be using String.charAt(int) (or use a for-each loop on String.toCharArray()). There's no need to increment by zero, and you only consider lower case letters (so call String.toLowerCase()). Like,

public static int method3(String words) {
    int count = 0;
    for (char ch : words.toLowerCase().toCharArray()) {
        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
            count++;
        }
    }
    return count;
}

Also, consider a more meaningful method name (like countVowels()).

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

If your method has a return type of String, then return a string:

public static String method3(String words){
 int count = 0;
//logic here
return "" + count; // The "" + will convert the count to a string, if that is what you want. 

}

You can also change the return type of the method, to return an int:

public static int method3(String words){
     int count = 0;
    //logic here
    return count;

    }

Based on your logic, if a string in the variable words has any of the vowels, then it will increment the count by one, if not, it will not increment it and return 0. If you are sending an empty string or a string with no vowels, the method will return 0.

Jeremy Then
  • 525
  • 2
  • 12
  • thank you this helped out a lot, however I am still struggling with returning the actual value of the count. No matter what happens it is still a 0 even after i changed the return type to match what I was returning. – Artvmvs May 14 '19 at 23:29
  • This Answer deserves credit, but see my other Answer (https://stackoverflow.com/a/56139520/1357094) that addresses other problems in your code (it relates to the original code in your Question, not the code you have since edited changes into your Question to since then). – cellepo May 14 '19 at 23:41
0
  • Move the return outside of loop (your code returning inside, explains why you always get 0)
  • toLowerCase each word Character, to account for case
  • Make a Set of all vowels, and check if it contains each character of word (see next bullet)
  • *Note that indexOf, which your code calls, will always return just the first occurrence's index; that will cause your code to not always return the correct answer, even after moving the return outside the loop. Your code using indexOf also causes it to have a quadratic O(n^2) runtime (where n is the length of word). My solutions here do not use indexOf, and are linear O(n) in runtime. Both your solution and mine are constant O(1) memory space.

As below:

public static String countVowels(String word){
  int count = 0;

  Set<Character> vowels
    = new HashSet<>(Arrays.asList("a", "e", "i", "o", "u"));
  for(int i = 0; i < words.length(); i++){
    if(vowels.contains(Character.toLowerCase(word.charAt(i)))) {
      count++;
    }
  }

  return "This is the number of vowels " + count;
}

Even Better (code style optimization) - use an enhanced-for-loop:

public static String countVowels(String word){
  int count = 0;

  Set<Character> vowels
    = new HashSet<>(Arrays.asList("a", "e", "i", "o", "u"));
  for(char curChar: word.toCharArray()){
    if(vowels.contains(Character.toLowerCase(curChar))) {
      count++;
    }
  }

  return "This is the number of vowels " + count;
}
cellepo
  • 4,001
  • 2
  • 38
  • 57