我不明白这个错误 'Syntax error on token ";", { expected after this token'

I do not understand this error 'Syntax error on token ";", { expected after this token'

目前我有错误 Syntax error on token ";", { expected after this token,在下面显示的代码中:

public class findKey {

private final static ArrayList<Integer> listofPrimes = new ArrayList<>();

//Number of Primes we want
private final static int N = 224;

for (int candidate = 2, count = 0; count < n; ++candidate) {
    if (isPrime(candidate)) {
        ++count;
        listofPrimes.add(candidate); 
    }
}

// Very Simple isPrime() Function
// Miller-Rabin Algorithm added for reference
private static boolean isPrime(int num) {

    if (num == 2 ) {
        return true;
    }
    if (num % 2 == 0) {
        return false;
    }
    for (int i = 3; i * i <= num; i += 2) {
        if (num % i == 0) return false;
    }

    return true;
}

public static int convertKey (String input) {

    int result; 
    char[] charArray = input.toCharArray();

    for (int i = 0; i < charArray.length; i++){
        result *= listofPrimes.get(convertInt(charArray[i]));
    }

    return result;
}

private static int convertInt (char a) {

    int ascii = (int)a; 
    int i = ascii - 32;

    return i;
}
}

我不明白错误是什么。我敢肯定这是非常愚蠢的事情,我正在消隐。对于这个问题,我们将不胜感激。

如有任何额外的详细信息,我们将予以答复。

你的 for 循环

for (int candidate = 2, count = 0; count < n; ++candidate) {
    if (isPrime(candidate)) {
        ++count;
        listofPrimes.add(candidate); 
    }
}

不在方法中。

根据您的评论出现

Can for loops not exist outside a method in a class? I just started programming in Java after C

您期望 static initializing block 的行为。像,

static {
    for (int candidate = 2, count = 0; count < n; ++candidate) {
        if (isPrime(candidate)) {
            ++count;
            listofPrimes.add(candidate); 
        }
    }
}