How do I write java code that will only accept capital A or B input from the user?

Code:
import java.util.Scanner;
public class ReadChar {
 
  public static void main(String[] args) {
 
    Scanner sc = new Scanner( System.in );
 
    String letter = "";
    while(true) {
      System.out.print("Please enter a letter: ");
      letter = sc.nextLine();
      if( letter.equals("A") || letter.equals("B")){
        break;
      }
      System.out.println("Try again.");
    }
    System.out.println("You entered: " + letter);
 
  }
}

Output:
$ java ReadChar
Please enter a letter: a
Try again.
Please enter a letter: s
Try again.
Please enter a letter: d
Try again.
Please enter a letter: B
You entered: B