Sieve of Eratosthenes Java while loop

I'm completing this code for my intro to Java class and I understand the concept of the sieve as well as the implementation while using a for loop. I'm trying to convert it into a while loop as per the algorithm given in class but I'm running into trouble with it where it terminates without printing. Can anyone help?

public class SieveOfEratosthenes {

    public static void main(String[] args) {
        //user input
        int n;
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter your number: ");
        n = sc.nextInt();
        sc.close();
        //initialize boolean array with numbers 2 to n
        boolean[] myArray = new boolean[n+1];
        for (int i = 2; i < n; i++) {
            myArray[i-1] = true;
        }
        int p = 2;
        while (p<=n ) {
            for(int i=p*p; i<=n; i++) {
                if(i%p==0) {
                    myArray[i-1] = false;
                    p=i+1;
                } else
                    myArray[i-1] = true;
                if(myArray[i-1])
                    System.out.print(i + " ");
            }
        }
    }
}