Why is it when I do this
while(word.charAt(left) == word.charAt(right) && left >= 0 && right < word.length()){
left--;
right++;
}
its says in the while conditions "left >= 0 && right < word.length()" condition doesn't reach, but when I do it like this
while(left >= 0 && right < word.length() && word.charAt(left) == word.charAt(right)){
left--;
right++;
}
The conditions are met.
Is there an order for while(loop conditions)???
The order does matter as conditions are checked from left to right. You're getting that message because if the previous condition evaluates to true, then those two conditions
left >= 0 && right < word.length()
will always evaluate to true. You probably want the latter (since you would want to check all the conditions).
Related
I have a use case where I have to check if one condition is subset of another condition. Meaning, the result set that satisfies condition 2 will be the subset of result set that satisfies condition 1.
I was checking and intellij does figures it out during code inspection
if (x < 7 && x > 1) {
return true;
} else if (x > 3 && x <6) {
return false;
}
Here during code inspection it does highlights that result of second condition is always false.
So, How it is implemented?
This range:
(x > 3 && x <6)
is a subrange of:
(x < 7 && x > 1)
so if the condition (x < 7 && x > 1)
is true then this condition (x > 3 && x <6) will not be reached
is false then this condition (x > 3 && x <6) is also false
So the meaning of code inspection is:
if (x > 3 && x <6) is reached then it is always false.
The right way to implement your logic should be to check first the subrange:
if (x > 3 && x <6) {
return false;
} else if (x > 1 && x < 7) {
return true;
} else {
return ....;
}
I am working on making a coffee ordering system. It takes inputs from users and calculates the price of their coffee. One of the inputs asked for is "syrupshots". This code always makes syrupshots = 3 even if the user properly chose a number according to the size of their coffee.
//if size == tall syrup shots <=3
if(size == 1 && syrupshots < 1 || syrupshots > 3)
{
syrupshots = 3;
}
//else if size == medium syrup shots <=5
else if(size == 2 && syrupshots < 1 || syrupshots > 5)
{
syrupshots = 3;
}
//else if size == Venti syrup shots <=7
else if(size == 3 && syrupshots < 1 || syrupshots > 7)
{
syrupshots = 3;
}
System.out.println(syrupshots);
I am not sure why syrupshots is always = 3 no matter what.
Not always, sometimes it returns 1 or 2. But never >3, like it looks like it should.
Your problem is with logical operator precedence. For example:
if(size == 1 && syrupshots < 1 || syrupshots > 3) {}
If size=3 and syrupshots=5 (a valid combination), then this if block is still entered because the && is evaluated first:
size == 1 && syrupshots < 1
and that equals false, so you have left (false || syrupshots > 3)
BUT then the || is evaluated, syrupshots > 3 is true, so the whole expression is true
You need to change the order of precedence by using brackets:
if(size == 1 && (syrupshots < 1 || syrupshots > 3)) {}
As one comment says, all conditions trigger syrupshots to be assigned three. Also, I don't think you wrote your test condiiotns correctly, judging from your comments.
if(size == 1 && syrupshots <= 3)
^That would be proper way to test for the check outlined in your comment
if(pl.y+pl.height >= a.y && pl.x+pl.width >= a.x+1 && pl.x <= a.x+a.width-1 && pl.y<=a.y) { //TOP
colUP=true;
}
else colUP=false;
if(pl.y <= a.y+a.height && pl.x+pl.width >= a.x+1 && pl.x <= a.x+a.width-1 && pl.y+pl.height>=a.y+a.height) { //BOTTOM
colDOWN=true;
}
else colDOWN=false;
if(pl.x <= a.x+a.width && pl.x+pl.width>a.x+a.width && pl.y+pl.height >= a.y && pl.y <= a.y+a.height){ //RIGHT
colRIGHT=true;
}
else colRIGHT=false;
if(pl.x+pl.width >= a.x && pl.x<a.x && pl.y+pl.height >= a.y && pl.y <= a.y+a.height){ //LEFT
colLEFT=true;
}
else colLEFT=false;
I setup a debug that will tell me which of the 4 Booleans is being set to true, and they don't show that when I put the box 'pl' on top of box 'a' colUP is not equal to true, and they will only come true in weird instances where box 'pl' is colliding with several box 'a's , and the collision for a certain side might be true when it isn't but if colUP is true then colRIGHT is true for some reason. (This code is inside a for loop that goes through an array list of Rectangles and sets the current Rectangle equal to the variable 'a' so that a.x is the box's x position)
You have right logic but you set false for each condition separately. In reality all conditions should be true. So, use one boolean variable - isInRectangle=true; then check all conditions - left,right,top,bottom. If any is not true then isInRectangle=false;
It is simple AND logical operation for all 4 conditions.
I took a programming class, and I'm revisiting old programs that I did not quite get right. This one is a Game Of Life program, and I have a question about code cleanup.
I need to make sure that an array element is in bounds before checking whether its neighbor's boolean value is true or false. I have a statement to check if firstGen[0][0]'s top-left (up one row, left one column) is in bounds. Is there an easier or more elegant way to check if an element is in bounds or to restrict the element checks to the boundaries of a given array without using four && conditionals per if statement?
Note that I have only changed the first if statement thus far, so there may be errors elsewhere. I also excluded the boundary checks for the other neighbors.
public static boolean[][] generation(boolean[][] firstGen)
{
int length = firstGen.length;
boolean[][] newGen = new boolean[length][length];
for (int j = 0; j < firstGen[0].length; j++)
{ for (int i = 1; i < firstGen.length; i++)
{
int count = 0;
if ((i-1 >= 0) && (i-1 < length) && (j-1 >= 0) && (j-1 < length)) //top-left element exists
{ if (newGen[i-1][j-1] == true) count++; } //increment `count` if top-left element is true
if ((newGen[i][j] == false) && (count == 3)) newGen[i][j] = true;
else if ((newGen[i][j] == true) && (count == 1)) newGen[i][j] = false;
else if ((newGen[i][j] == true) && (count > 3)) newGen[i][j] = false;
else break;
}
}
return newGen;
}
If i and j are in bounds, then you know for sure that i - 1 < length and j - 1 < length are both true.
Also:
i - 1 >= 0 can be written i > 0
if (condition == true) can be rewritten if (cond)
So you could replace:
if ((i-1 >= 0) && (i-1 < length) && (j-1 >= 0) && (j-1 < length)) //top-left element exists
{ if (newGen[i-1][j-1] == true) count++; } //increment `count` if top-left element is true
by:
//increment `count` if top-left element is true
if (i > 0 && j > 0 && newGen[i-1][j-1]) count++;
That's the best way I can think of to check if its out of bounds, but an alternative method in general, and one that I think gives programs like the Game of Life more exciting outcomes, is adding periodic boundaries. Basically this means that if you walk off one edge, you end up on the other side (like in pac-man). It sounds complicated, but really all it takes is the % function, which returns the remainder of division between the two numbers given.
So:
27 % 5 = 2;
So for adding periodic boundries you would update x and y positions like this:
x = (x + xStep + horizontalSize) % horizontalSize;
y = (y + yStep + verticalSize) % verticalSize;
Where xStep and yStep are +1 or -1 depending on what direction you want to go. (this works nicely with a for loop) The addition of the size is to make sure you go below zero when you get close to borders.
Then you never have to worry about messy border conditions, everything simply overlaps. No need to check each and every border. I hope this makes sense. Please ask for clarification if not. I've used this more for random walker programs but the idea is the same.
I dont know why this stopping criteria keeps failing but I believe it has something to do with the logical or (||). I debugged and it doesnt seem to stop where I want it to, which is on when cp (current position) is on the perimeter of the array.
if(cp == start || (cp.getColumn() > 0 || cp.getColumn() < maze[0].length-1 || cp.getRow() > 0 || cp.getRow() < maze.length+1)){`
This is a for a maze solving algorithm. Basically, the maze is a 2D array made up of X's for walls. Every cell with an X is a wall and the 'current position' can not access it. The mission of the 'current position' is to get to out of the maze which means it either has to be in the first or last column or first of last row. The algorithm works very well but just doesn't recognize when the current position is at the index of the exit.
I'm sorry for being so vague, I've been working on this for so long...
So, none of the following things is true:
cp == start
cp.getColumn() > 0
cp.getColumn() < maze[0].length - 1
cp.getRow() > 0
cp.getRow() < maze.length + 1
But you think (at least) one of them should be true. Which one?
Being or statements, that extra set of parentheses are redundant:
if(cp == start || cp.getColumn() > 0 || cp.getColumn() < maze[0].length-1 || cp.getRow() > 0 || cp.getRow() < maze.length+1){
I can't see exactly what you are doing, but I hope this helps at least...
I con't really see what you're trying to do here, but guessing that 'maze' is a two dimensional array, it should probably be maze.length-1 in the last comparison. However, this would make the if statement evaluate to true when cp is NOT on the perimeter of the array.
It appears you are using the start conditions in your OR set. You may want to put an ! (Not) before the first bracket. As written, I think the else clause would be where your stop should occur.
Consider wrapping the processing code in this if statement.
Desk check your logic to see when you exit. Remember that OR stops processing on the first TRUE, and AND stop processing on the first FALSE.
I'm not sure you've copied your code correctly
if(cp == start
|| (cp.getColumn() > 0 || cp.getColumn() < maze[0].length-1
|| cp.getRow() > 0 || cp.getRow() < maze.length+1)) {
For simplicity of understanding pretend that maze.length[0]-1 evaluates to, say 3, then consider this bit:
cp.getColumn() > 0 || cp.getColumn() < 3
That will be true for every value of the cp.getColumn(), try say 8, yep, bigger than 0, -99, yep it's less than 3. I'll bet you meant
cp.getColumn() > 0 && cp.getColumn() < 3
this will be true if the column is in the maze. If you want to test for out of the maze:
cp.getColumn() <= 0 || cp.getColumn() >= 3
(Side note I use <= 3, not just == 3 deliberately, this deals with cases such as move of more than one step.)
If you want to test if it's in the maze you have
if(cp == start
|| (
cp.getColumn() > 0 && cp.getColumn() < columnCount
&& cp.getRow() > 0 && cp.getRow() < rowCount)) {
if you want to test if it's outside the maze
if(cp != start
&& (
cp.getColumn() <= 0 || cp.getColumn() >= columnCount
|| cp.getRow() <= 0 || cp.getRow() >= rowCount)) {