Another question how do I make it so the one rectangle cant enter the other one?

Make it so if they intersect (lets say you're moving to the right) then the moving rectangle's coordinate's would be "current coordinate - 1". so like this: (but you will also have to do a check to see which direction the player is going)

a is the player

if(a.intersects.b){

if(player_is_moving_right)

a.setX(a.getX() - 1);

}

And you would have to do the same thing for up, down, left, and right.

I preferred a slightly different approach to this problem.

What I did / do is :

Take the speed the object is travelling at, use that to determine the coordinates where the object will be after the frame, draw a line from each relevant point, find the intersection (if any) between the lines and the object it will hit, drop the speed down so that it will stop at the edge and flag the collision.