Scan objects while moving?
I'm not a leJOS expert, but from looking at the docs, I don't think that the rotateTo method works the way that you are expecting:


  public void rotateTo(int limitAngle)
  
  Rotate to the target angle. Do not return until the move is complete.


So here's what your code is actually doing:

Motor.A.rotateTo(40); // Rotate to 40 and return when rotation is finished
while(Motor.A.isMoving()) { // The motor won't be moving
    // Execution never gets here
}


The rotateTo method takes an optional immediateReturn parameter that allows it to return without waiting for movement to complete:


  immediateReturn - if true do not wait for the move to complete


Replace your calls to rotateTo(angle) with rotateTo(angle, true), and you should be on the right track. Something like this should be more effective:

Motor.A.rotateTo(40, true); // Begin rotating to 40 and return immediately
while(Motor.A.isMoving()) { // The motor will be rotating for a time
    // Execution should loop here until the motor stops
}