I have a bunch of characters and want to remove everything that isn't a '#' '.' 'E' and 'G'.
I tried to use this:
if (buffer.get(buffertest) == 'G'|'E'|'#'|'.')
But got an issue with an incompatible type.
This root problem is incorrect use of the bitwise OR operator, and the Java operator precedence hierarchy. Java expressions of this type are evaluated left to right, and the == operator takes precedence over |. Which when combined, your expression roughly translates to:
(buffer.get(buffertest) == 'G') | 'E' | '#' | '.'
The first part of the expression buffer.get(buffertest) == 'G' evaluates to a boolean.<br>
The second part of the expression'E' | '#' | '.'` evaluates to an int, which is narrowed to a char
Which leads to an incompatible type compile time error. You can correct your code by expanding the check this way:
char ch = buffer.get(buffertest);
if(ch == 'G' || ch == 'E' || ch == '#' || ch == '.') {
// do something
}
You need to compare for each character individually. Assuming that buffer.get(buffertest) returns a char, here's how to do it:
char c = buffer.get(buffertest);
if (c == 'G' || c == 'E' || c == '#' || c == '.') {
// do something
}
Alternatively, you could do something like this:
char c = buffer.get(buffertest);
if ("GE#.".contains(Character.toString(c))) {
// do something
}
You haven't shown the type of buffer, which makes things harder. But assuming buffer.get returns a char, you could use:
if ("GE#.".indexOf(buffer.get(buffertest) >= 0)
Or you could check each option explicitly, as per Simulant's answer... or to do the same thing but only calling get once:
char x = buffer.get(buffertest);
if (x == 'G' || x == 'E' || x == '#' || x == '.')
Your original code is failing because | is trying to perform a bitwise "OR" operation on the four characters... it's not the same thing as performing a logical "OR" on four conditions.
if (buffer.get(buffertest) == 'G'||
buffer.get(buffertest) == 'E'||
buffer.get(buffertest) == '#'||
buffer.get(buffertest) == '.')
Related
Short version
When pressing <enter> at the end of a // comment, Intellij sometimes decides to continue the // comment on the next line. How can I prevent that? Is there a setting somewhere to disable this automation?
Long version
There is a thing I do regularily, it is to break a long expression with a double-slash.
Let's say I have a line like
boolean isHex = c >= '0' && c <= '9' || c >= 'A' && c <= 'F' || c >= 'a' && c <= 'f';
and I want to split it like that
boolean isHex = c >= '0' && c <= '9' //
|| c >= 'A' && c <= 'F' //
|| c >= 'a' && c <= 'f';
Note that I want the final // in order to prevent any formatter to join the lines again.
So I insert a double-slash-return after the '9', by pressing //<enter>. But Intellij will auto-continue the comment on the next line.
boolean isHex = c >= '0' && c <= '9' //
// || c >= 'A' && c <= 'F' || c >= 'a' && c <= 'f';
It forces me to uncomment and reindent the line manually.
I want Intellij to not continue the comment on the next line and optionally indent my code:
boolean isHex = c >= '0' && c <= '9' //
|| c >= 'A' && c <= 'F' || c >= 'a' && c <= 'f';
So I want to disable this "continue // comment after <enter>" feature. Is it possible? I haven't found any setting related to that.
The closest you are going to get is to define a macro to insert a new line and remove the comment and then bind that macro to a suitable key.
Go to Settings → Code Style → Java → Wrapping and Braces and check "Line breaks" under "Keep when reformatting". This will make IntelliJ's formatter respect any manual line breaks, even if they contradict other formatting rules.
This question already has answers here:
Why does non-equality check of one variable against many values always return true?
(3 answers)
Closed 4 years ago.
I have this loop statement, which I'll express using C-like syntax (C, C++, Java, JavaScript, PHP, etc. all use similar syntax):
while (c != 'o' || c != 'x') {
c = getANewValue();
}
I want it to run until I get a 'o' or 'x', but it never exits, even when c is 'o' or 'x'. Why not?
I've also tried using if:
if (c != 'o' || c != 'x') {
// Show an error saying it must be either 'o' or 'x'
}
but that also always shows the error message, even when c is 'o' or 'x'. Why?
The condition (c != 'o' || c != 'x') can never be false. If c is 'o', then c != 'x' will be true and the OR condition is satisfied. If c is 'x', then c != 'o' will be true and the OR condition is satisfied.
You want && (AND), not || (OR):
while (c != 'o' && c != 'x') {
// ...
}
"While c is NOT 'o' AND c is NOT `'x'..." (e.g., it's neither of them).
Or use De Morgan's law, covered here:
while (!(c == 'o' || c == 'x')) {
// ...
}
"While it's NOT true that (c is 'o' or c is 'x')..."
It must be if(c!='o' && c!='x') instead of if(c!='o' || c!='x'). If you use the or operator the boolean expression will be always true.
Why is my c != 'o' || c != 'x' condition always true?
The expression combines two sub-expressions using the logical OR operator (||). The expression is true if at least one of the sub-expressions is true. In order to be false, both sub-expressions it connects must be false.
The sub-expressions are c != 'o' and c != 'x'.
The first sub-expression c != 'o' is false when c == 'o'. The same for the second one; it is false when c == 'x'.
Please note that they cannot be false on the same time because c cannot be 'o' and 'x' on the same time.
The condition should be if(!(c=='o' || c=='x')) or if(c!='o' && c!='x')
even when you enter x or you enter o in that case if condition evaluates to true and hence game_start remains false.
it should be if(c!='o' && c!='x')
or use a more straight forward way
if(c == 'o' || c == 'x')
game_start=true;
else
System.out.println("you can only enter o or x!");
There is a code like this :
boolean isValid(int ch) {
if(ch < '1' | ch > '7' & ch != 'q') return false;
else return true;
}
class HelpClassDemo {
...
do {
choice=(char) System.in.read();
} while(!hlpobj.isValid(choice));
}
That's the question :
Why we used int ch in isValid(int ch) in spite of choice's type is char?
Shouldn't we use char ch ? If we should use int ch why there is a code like this : (ch < '1' | ch > '7' & ch != 'q')
Isn't ch<1 or ch>7 logical ? I know it's a quite simple question but I'm confused about this.
A int type as a wider range than a char type (see this data type range table). IMO, receiving a char as an int provides you with some kind of overflow protection (points of view on this are welcome) but it is not something I'd do as I'd use the right data type instead.
Now, when you compare
if(ch < '1' | ch > '7' & ch != 'q') return false;
you are implicitly casting '1' and '7' to int type, which is perfectly valid.
And finally, regarding
Isn't ch<1 or ch>7 logical ? I know it's a quite simple question but
I'm confused about this.
It is logical, but it's not the same.
If you do ch > '1' && ch < '7', you are comparing ch to the ASCII value of 7, which is 55, and value of 1, which is 48. Basically, you are making sure that ch is a char between 2 and 6, both inclusive.
But if you do ch > 1 && ch < 7, you are comparing integers and validating that the char represented by ch is between 2 and 6 (both inclusive). This char is very likely to be not-human-readable.
If you want to make them equivalent, you wold have to compare according to the appropiate char value, like
if (ch > 48 && ch < 55)
The method Character.isLetter(Char c) tells whether the character is a unicode letter. What if I want to check for English letters (a-zA-Z) without regex.
Easy
char c = ...;
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
//english letter
}
please ignore the question - its wrong
I am not sure if my question is issue is related to operator precedence- Just to rule out that I added additional bracket. My understanding is in that case that code in each bracket will be executed. So basically all the OR operation will happen and its output would be AND'ed to condition a.
I have below set of parameters a = true and c = 254 , b is not availble ( b is initialized to 0 -At any given time either b or c only is availble) . So for the above condition I am expecting if condition to result in true but it's resulting in false condition. Any reason why ? Also what is best way to debug such things as in where exactly condition is going wrong - any pointers
if ((a == true) && ((b == 460) || (b == 454) || (b == 455) ||
(c> 13568 && c< 14335) ||
(c> 10640 && c< 10655) ||
(c> 11296 && c< 11311) ||
(c> 25600 && c< 26111) || (c== 7825)))
First a is evaluated, if (a == true) evaluated to true, then only it will execute next && statement
((b == 460) || (b == 454) || (b == 455) ||
(c> 13568 && c< 14335) ||
(c> 10640 && c< 10655) ||
(c> 11296 && c< 11311) ||
(c> 25600 && c< 26111) || (c== 7825))
Inside this, it will check for any one condition which is true, and once it encounter any one statement true, it return from there.
For your condition to be true, a must be true, and in addition, at least one of the conditions on b or c must be true.
Therefore, if a==true and c==254, you will get false, since c is not within any of the ranges you allow, and, as you said, b is not available (which I'm assuming means it doesn't have one of the 3 values you allow).
It would be much simpler if the code is written in a more readable manner;
bool isEqualToAny(int valueToCheck, int[] listToCheckIn){
boolean isMatch = false;
(for item in listToCheckIn){
if (item == valueToCheck){
isMatch = true;
break;
}
}
}
bool isWithinRange(int valueToCheck, int min, int max){
return (valueToCheck > min && valueToCheck < max);
}
if ((a == true)
&& (isEqualToAny(b, int[]{460,454,455})
|| isWithinRange(c,3568,14335)
|| isWithinRange(c,10640,10655)
|| isWithinRange(c,11296,11311)
|| isWithinRange(c,25600,26111)
|| isWithinRange(c,10640,10655)
|| (c== 7825)))
In java8 you can use an array of Tuples to make #isWithinRange more like #isEqualToAny