Problem in code

This is my error
NZEC
Exception in thread “main” java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at june_challenge1.main(Main.java:7)

This is my code:
import java.util.*;
class june_challenge1
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(int k =0;k<t;k++)
{
int d= sc.nextInt();

    String str= sc.next();
    char arr[] = new char[d];
    arr = str.toCharArray();
    int sum =0;
    int count =0;
    for(int j =0;j<d;j++)
    {
        if(arr[j] == 'P')
        sum++; 
    }
        float p = (float)sum/d;
        if(p>=0.75)
        {
            System.out.println(0);
            break;
        }
        if(d<=4)
        {
            System.out.println(-1);
            break; 
        }
        int wsum = (int)((Math.ceil(0.75*d)));
        for(int i =0;i<d;i++)
        {
            if( i>1&&i<d-2)
            {
                if(arr[i]=='A')
                {
                if((arr[i-1]=='P'||arr[i-2]=='P')&&(arr[i+1]=='P'||arr[i+2] =='P'))
                {
                    sum++;
                    count++;
                }
                }
            }
            if(sum==wsum)
            {
                System.out.println(count);
                break;
            }
        }
        if(sum<wsum)
        {
            System.out.println(-1);
                break;
            }
        }
    }
}

Whats the fix?

try to use new Scanner class object to accept the String data

Sometimes it only shows when u try compiling on codechef. If your code works fine on java ide then u can directly submit it to codechef.