Java – why doesn’t my code compile which string starts with a vowel check?
•
Java
if (flipped.charAt(0) = "a" || "e" || "i" || "o" || "u"){
if (flipped.charAt(0) = "a" || "e" || "i" || "o" || "u"){ paren = "(" + flipped; String firstpart = paren.substring(0,5); String rest = paren.substring(5); System.out.println(rest+firstpart); }
In this code, I want to check whether the first character flipped by string is a vowel If so, I add a bracket at the beginning and move the first five characters to the end of the string Eclipse gave me Java Lang.nullpointerexception and say "the left side of the assignment must be a variable" What can I do to solve this problem?
Solution
Use a collection that contains all 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 (although error: = will be assigned, = = will be compared)
if (flipped.charAt(0) == "a" || "e" || "i" || "o" || "u"){
We will first compare flipped Charat (0) = = "a", which returns a Boolean value Then it will continue Boolean | "e" | "I" | "O" | "U"
Boolean "e" is not a valid code
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码