-2

Trying to make it so that if the user enters any vowel capitalized, it will return true. So, that's where I added the "letter = letter.toLowerCase();" but it's not working still..

    public static boolean isVowel (String letter) {
        letter = letter.toLowerCase();
        if (letter == "a" || letter == "e" || letter == "i" || letter == "o" || letter == "u" ) {
            return true;

        } else {
            return false;
        }
    }
jayweezy
  • 81
  • 8
  • See the [linked question](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) for why you never use `==` with strings. Instead, use `.equals("a")` etc. – T.J. Crowder Feb 21 '20 at 18:42
  • If you were to use any decent IDE, it will warn you about doing String comparison using `==`. You will save yourself a lot of time and energy in the long run. I suggest you stop using whatever you're currently using. Install IntelliJ. If you have any aspiration to be a professional programmer, you will likely end up using it. May as well start now. – Michael Feb 21 '20 at 18:44
  • I'm using Eclipse at the moment. Is that bad? – jayweezy Feb 21 '20 at 18:49

1 Answers1

2

Try using if (letter.equals("a") || and so on.

== tests the memory references of String variables, not the contents of the references.

The equals method of the String class tests the contents of the references.

Mr Morgan
  • 2,215
  • 15
  • 48
  • 78
  • 1
    Perhaps using the `equalsIgnoreCase()` method would be better in this case? – hooknc Feb 21 '20 at 18:44
  • Perhaps. It is up to the op. – Mr Morgan Feb 21 '20 at 18:44
  • @hooknc How would I use the equalsIgnoreCase()? – jayweezy Feb 21 '20 at 19:36
  • Also, you can avoid chain of conditional statement in your code using something like this : public class Test { private static final String vowelStr = "aeiou"; public static void main(String [] args) { boolean isVowel = isVowel("a"); System.out.print(isVowel); } public static boolean isVowel (String letter) { return vowelStr.contains(letter) ? true : false; } } – MOnkey Feb 22 '20 at 06:29