user3072276 user3072276 - 8 months ago 21
Java Question

Why won't my code compile which checks if a string begins with vowel?

if (flipped.charAt(0) = "a" || "e" || "i" || "o" || "u"){
paren = "(" + flipped;
String firstpart = paren.substring(0,5);
String rest = paren.substring(5);

In this code, I'm looking to check if the first character of String flipped is a vowel. If it is, I'm adding a parenthesis to the beginning and moving the first 5 characters to the end of the string. Eclipse is giving me
and saying that "The left-hand side of an assignment must be a variable." What can I do to fix this?


Use a collection that holds all of these values.

Set<Character> myList = new HashSet<Character>(Arrays.asList('a', 'e', 'i', 'o', 'u'));

if(myList.contains(Character.toLowerCase(flipped.charAt(0)))) {
   // Do work

This line of code (while wrong: = will assign, == will compare)

if (flipped.charAt(0) == "a" || "e" || "i" || "o" || "u"){

will first compare flipped.charAt(0) == "a" which returns a boolean. Then it will continue with boolean || "e" || "i" || "o" || "u".

boolean || "e" is not valid code.