Java Arrays and Loops: The Prime Directive: struggling with error messages

I am sure I’m missing something really simple in the following code but I just can’t see it! Currently I’m getting ‘error: class, interface or enum expected’. I have tried Googling for half an hour and am even more confused.

Any help is much appreciated :slight_smile:

// Import statement:
import java.util.ArrayList;

public class PrimeDirective {

// Add your methods here:
public boolean isPrime(int number) {
if (number == 2) {
return true;
} else if (number < 2) {
return false;
}

for (int i = 2; i < number; i++) {
if (number % i == 0) {
return false;
}
}
return true:
}
}

public static void main(String args) {

PrimeDirective pd = new PrimeDirective();
int[] numbers = {6, 29, 28, 33, 11, 100, 101, 43, 89};

System.out.println(pd.isPrime(7));
}

}

Hi,

Would love to help.
There’s a typo and maybe a few curly brace issues but it’s hard to tell. Try pasting your code again with the </> format button and it should be easier to detect what’s happening.

The actual error message is alluding to the fact that you’re probably closing out your class before your main method starts (by accident, but hard to tell with this formatting).

There will be other errors though due to formatting. You might want to do this in a text editor like VS code or sublime (you might have to set up a java compiler for these, I’m not sure.

Thanks for replying! Like this?

(apologies if I’m a little scarce, currently on break at work)

// Import statement:
import java.util.ArrayList;

public class PrimeDirective {

// Add your methods here:
public boolean isPrime(int number) {
if (number == 2) {
return true;
} else if (number < 2) {
return false;
}

for (int i = 2; i < number; i++) {
if (number % i == 0) {
return false;
}
}
return true:
}
}

public static void main(String args) {

PrimeDirective pd = new PrimeDirective();
int numbers = {6, 29, 28, 33, 11, 100, 101, 43, 89};


System.out.println(pd.isPrime(7));
}

}

Hi,
I played a little with your indentation. Barring the syntax error (which your compiler should point out), I pointed out where the main method is being left out.

Especially for troubleshooting it helps to clean up the formatting.

import java.util.ArrayList;

public class PrimeDirective {


	public boolean isPrime(int number) {
		if (number == 2) {
			return true;
		} else if (number < 2) {
		return false;
		}

		for (int i = 2; i < number; i++) {
			if (number % i == 0) {
				return false;
			}
		}
		return true:
	}
} //Class prime directive is ending here. This means it won't have a main method...


public static void main(String args) {

	PrimeDirective pd = new PrimeDirective();
	int numbers = {6, 29, 28, 33, 11, 100, 101, 43, 89};


	System.out.println(pd.isPrime(7));
}

}

Hello!

Thank you so much, i knew it was something daft like that. And apologies for the screwy formatting: i was in a hurry to post but that’s no excuse!

No problem! It happens :slight_smile: