Why won't my code compile which checks if a st

2019-07-07 09:03发布

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'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 java.lang.NullPointerException and saying that "The left-hand side of an assignment must be a variable." What can I do to fix this?

5条回答
叛逆
2楼-- · 2019-07-07 09:16

Your code has following issues,

  1. Use conditional operator == instead of assignment = at if statement.
  2. Use single quotation ' instead of double " for char
  3. Make a separate method for vowel check.

    boolean isVowel(char ch){
      ch=Character.toLowerCase(ch);
      return ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u';
    }
    
查看更多
Ridiculous、
3楼-- · 2019-07-07 09:33

The accepted answer although explained the problem didn't quite show the solution for how he was checking the problem. So I figured i'd show a corrected solution as well as offer my own solution to such a problem.

Someone who cannot understand Boolean compare syntax isn't going to understand All those special classes. Not to mention some of those needs imports he may not have and will now need to understand why he's getting errors. I assume this person has came to a resolution by now given it's been 5 years.. but in the evnet someone else or even this person still is unsure on something.

Your Original Code Updated ( I removed the contents inside as I don't know what they do or if they were accurate ).

char c = flipped.charAt(0);
if (c == 'a' || c == 'A' || c == 'e' || c == 'E' || c == 'i' || 
    c == 'I' || c == 'o' || c == 'O' || c == 'U' || c == 'u')
{

Now this supports checking if "flipped.charAt(0)" equals a vowel weather lower case or uppercase. As you can see we do a Boolean check for each situation by checking if "C" equals something else. You only offered the check one time so the syntax error was because of that you were doing Boolean checks on non Boolean values. When you have values next to "||" it must be "false", "true" or "SomethingA == SomethingB". If that something is an object you typically have to do "SomethingA.equals(SomethingB); E.g. byte,int,short,long,float,double will all work just fine, but String would require the second method.

Below are some tips to reduce this further.

We can force char "c" to lowercase by doing any of the below methods.

char c = Character.toLowerCase(flipped.charAt(0));

Or we can do a more clever way.

char c = flipped.charAt(0) | 32;

As such now we only need to do the following to check if it's a vowel.

char c = flipped.charAt(0) | 32;
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' ||c == 'u')
{

However we can take this a step further.

We can reduce the code even more!

if (((1 << flipping.charAt(0)) & 2130466) != 0)
{

So basically how my final solution works. Unfortunately it's pretty involved to explain it, but i'll try my best.

In any programming language you have values of Byte, Short, Int, and Long these are 8bit, 16bit, 32bit, and 64bit respectively.

When you perform 1 << N you are doing 2^N which is basically the power of two method. The thing is though when you use this on the Byte, Short, Int, or Long the value "N" is reduced.

So.. (keep in mind different languages handle these differently).

Byte can only range from 0-7.
Short can only range from 0-15.
Int can only range from 0-31.
Long can only range from 0-63.

So now we know letters have a value A-Z = 65-90 and a-z = 97-122 when we do 1 << letter it will actually be 1 << (1-26) because the those numbers module or remainder of 32 is 1-26 in both cases.

You can see this by doing the following.

A = 65.
65-32=33.
33-32=1. Stop.

So now we know A will equal 1 in this situation.

So now we do 1 << 1 or 2^1 = 2. So the letter A gives us the value "2".

Repeat this for all the vowels and we can a sum of bit values. Bit values are just powers of two added together. I again really can't go hard explaining this it's pretty involved but hopefully you kind of have an idea.

Now what we are doing is taking the sum of the vowel bits and comparing it to the number 2130466 which contains the bit values of A,E,I,O,U already. If those bit value we check for happens to exist in 2130466 then it must be A,E,I,O,U and as such it's a vowel.

The return result is 0 or the value so we simply check that this value doesn't equal 0.

Please keep in mind if anyone uses this that this assume you know the letter will be A-Za-z situation because if it was for example a "!" this will return a false positive as a "A" vowel. You can solve this by prechecking if the value is below "A" and above "u" and return out early.

查看更多
4楼-- · 2019-07-07 09:37

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.

查看更多
我只想做你的唯一
5楼-- · 2019-07-07 09:38

Another very simple solution I often use:

if ("aeiou".indexOf(Character.toLowerCase(text.charAt(0))) >= 0) {
    // text starts with vocal.
}
查看更多
兄弟一词,经得起流年.
6楼-- · 2019-07-07 09:38

You can also use regular expression matching:

if (text.matches("^[aeiou].*")) {
查看更多
登录 后发表回答