Prime Directive project, bug need help :(

https://www.codecademy.com/courses/learn-java/projects/java-prime-directive

I’m doing a Prime directive project and am stuck on a bug that I am confused about.

I made the code…

import java.util.ArrayList;

class PrimeDirective {
public static void main(String args) {

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

public boolean isPrime(int number) {
for (int i = 2; i < number; i++){
if (number % i == 0) {
return false;
}
if(number == 2) {
return true;
} else if (number < 2){
return false;
System.out.println(pd.isPrime(6));
}
}
}
}
}
}

And when I put java PrimeDirective in the terminal so I can get an output nothing comes out. So then I put javac PrimeDirective.java to see the mistake I made, I get this

PrimeDirective.java:10: error: illegal start of
expression
public boolean isPrime(int number) {
^
PrimeDirective.java:24: error: class, interface
, or enum expected
}
^
2 errors

I’m confused about how I made an illegal start. How do I fix the mistake and make the starting not illegal? Also on how I made an interface, but an interface on what?

I’ll say the same thing as before.

When sharing code, make sure it’s an exact copy. Otherwise you’re sharing something other than what you’ve got and that’s no good.

Start with a minimal program. Make small edits from there.
You might for example start with printing out each value of your array, because you do need an array of numbers and you do need to do something with each number, so printing them out one at a time is a good way of checking that you’ve at least got that part in working order.

Then you might create a method, and perhaps you’d put nearly nothing in it, just return true. You should now be able to call that method and print the result for each value. The result is always true so far, but that still tests a whole lot of new things.

To test whether something is prime you’d try a lot of different divisors. Same thing again, do only a small part of it. Print out the divisors that you’re going to test, and then return true same as before.

…then you might add a condition, there’s something to test. You might print out the result of that.
… and so on until you’re done.


You’ve got problems throughout your code, so you’re writing something, not testing it, and then writing another thing, and at the end of it you’ve got a whole lot of problems, you run it, and then you don’t know what to do and there’s no way to proceed. Take the approach described above instead. Solve one small problem at a time, verify that it’s doing it’s job, pick another small problem.


javac isn’t just for checking for problems, javac is what compiles your code. if you make a change you will need to compile the code before you can run the new program, otherwise you’d be running the previous thing you compiled.

to run your code after editing, you therefore need to use both javac and java. you can tell bash to run one after the other like so:

javac mycodefile.java && java myclass

&& only executes the second part if the first part succeeds, so it would only run if it successfully compiles.

Thank you for the advice, and helping me. But the thing is that I am sending you the exact thing from my project. And also I am using the small steps it’s giving me, and doing them step by step. But then it’s coming out as wrong, even if I look for the hints for help, and do what the hint is telling me to do, I still get it wrong.

Were you running your code between each such step and seeing it do what you expected? Because that’s the important part. That’s how you know it’s okay to continue with next thing.

Well kinda, because if I don’t do all the things needed nothing is going to come out.

At a minimum you’d ensure it successfully compiles. I also made some suggestions about what you can print out as partial results.

You can also use dummy values in place of bigger things. I made a suggestion of that kind as well.

Okay Thanks for the suggestions, but can you explain to me what an illegal start is?

That’s only part of that sentence.

No expression starts out that way.


And if you’re well in control of what’s going on then you can take a look there and go, yep, that’s not a thing, fix it, move on.

But you’re not. Nor would I claim it’s expected.
…which is why -> small steps
that lets you avoid being able to look at the whole thing, it lets you deal with one thing at a time. Whenever there’s a problem, you know it’s got to do with the very small change you just made, because you successfully ran it prior to making it.

So staring an expression like this, isn’t illegal, and if so then why does it keep telling me this and now let my code go through?

public boolean isPrime(int number) {
}

If you did this:

5 + public boolean isPrime(int number) {
}

You would get a similar error message. Or at least, it’s of similar nature.
Because what does addition between two things look like? Two expressions with a + between them.

But in this case where does the 5 come from?

I wrote it.

The thing to realize is that you’re not told what the mistake is. You’re told about the invalid state that the parser is in.

And I’d have you reason about small changes in code instead, so that you’re looking at one small specific thing after the other. That way you know what the mistake is, because you just made it.

When you get error messages you can also see how the parser/compiler is reacting to various mistakes, so you would at the same time be learning to deal with error messages.

But then why would it tell you that it expected a certain thing?

If you write

5 +

then what’s expected to come next?

public boolean? probably not.

Then what would you expect?

some number, what else could possibly go there?

Well I get that, but then how would I make my method to work, I obviously wouldn’t but a number there.

Nor would you because you didn’t write 5 +

So in the context of what you had written up until then, what would be valid?

The question that matters isn’t what did the parser expect.

What matters is, what did you mean?

…small steps. go from one valid piece of code to another one small change at a time.

So… I wanted to make a method that was boolean, so it would return a certain value if it was false, or true. And wanted variables int and number. Wait is it not working because I didn’t give a value to the variables?