Reputation: 2342
Currently I have the error Syntax error on token ";", { expected after this token
, in the code shown below:
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;
}
}
I do not understand what the error is. I am sure it is something very stupid that I am blanking over. All help will be appreciated with this problem.
Any extra details will be answered if asked.
Upvotes: 1
Views: 879
Reputation: 201447
It appears based on your comment
Can for loops not exist outside a method in a class? I just started programming in Java after C
that you expect the behavior of a static
initializing block. Something like,
static {
for (int candidate = 2, count = 0; count < n; ++candidate) {
if (isPrime(candidate)) {
++count;
listofPrimes.add(candidate);
}
}
}
Upvotes: 1
Reputation: 63094
Your for loop
for (int candidate = 2, count = 0; count < n; ++candidate) {
if (isPrime(candidate)) {
++count;
listofPrimes.add(candidate);
}
}
is not in a method.
Upvotes: 2