The Prime Directive

So, I don’t know if this is a bug or not, but nothing’s printing to the terminal for me. For context, I decided to test myself a bit by seeing if I could write some code that successfully does the task without following the steps given, and nothing I can see really seems wron, per se, but in spite of that literally nothing is printing to the terminal, not even the test message I put in that should go even if I mucked up my code such that nothing else prints. I’m not even getting error messages. For reference, here’s my code:

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

class PrimeDirective {
  
  // Add your methods here:
  public void getPrimes(int[] numbersList) {
    ArrayList<Integer> primes = new ArrayList<Integer>();
    for (int number : numbersList) {
      for (int divisor = 2; divisor == (number + 1); divisor++) {
        if ((number % divisor) == 0 && divisor == number) {
          primes.add(number);
        }
      }
    }
    System.out.println(primes);
  }
  
  public static void main(String[] args) {

    PrimeDirective pd = new PrimeDirective();
    int[] numbers = {6, 29, 28, 33, 11, 100, 101, 43, 89};
    System.out.println(numbers);
    System.out.println("Testing");
    pd.getPrimes(numbers);
  }  
}

Has it bugged, or did I actually muck something up without realising? Any help is appreciated, thanks.

EDIT: Noticed I left out the () on getPrimes, but that didn’t solve anything either, so I’ve still got nothing.

EDIT2: Okay, I’ve just tested something else, adding in an input to the getPrimes method, and it has printed stuff, but not what I was expecting, I’ll edit the above code to show the new, and put the output I’m getting below here:

[I@2aae9190
Testing
[]

EDIT3: Fixed it. Turns out, part of my logic was backwards. Also, I hadn’t realised that a method that returns something had to have at least one of it’s possible return statements outside of any loops of if-else statements, even if those loops or if-else statements covered everything. Fixed that, and now it works.

I’m struggeling aswell. I get “true” no matter what. Cleared the whole project, still I get true. Wrote “hello world” code; true…

Could you post your code here then? I think I might know what your problem is, assuming I’ve figured rightly.

import java.util.Arraylist;
class PrimeDirective {
  
  public boolean isPrime(int number) {
    if (number == 2) {
      return false;
    } else if (number < 2) {
      return false;
    }
    
    for (int i = 2; i < number; i++) {
      if (number % i == 0) {
        return false;
      }
      else if(){
        return true;
      }
        }
    }
    

  }
  

  public static void main(String[] args) {

    PrimeDirective pd = new PrimeDirective();
    int[] numbers = {6, 29, 28, 33, 11, 8, 101, 43, 89};
    System.out.println(pd.isPrime(6));
  }  

}

Ok, so first, this bit:

shouldn’t be false, so that’s an issue. It’s obviously not what’s causing you to only get true, but it is still technically wrong.

Next:

This is probably your issue. It’s causing your program to assume true at the first time (number % i != 0). I’d suggest cutting that bit of the code entirely, leaving only the original if, and then after that if closes, then returning true. See if that helps any.

Thanks!

Now I dont get any answere when I write java PrimeDirective.

When I write javac Primedirective.java I get this:
Primedirective.java:25: error: class, interface, or enum expected
public static void main(String args) {
Primedirective.java:28: error: class, interface, or enum expected
int numbers = {6, 29, 28, 33, 11, 8, 101, 43, 89);
Primedirective.java:29: error: class, interface, or enum expected
System.out.println(pd.isPrime(6));
Primedirective.java:25: error: class, interface, or enum expected
}
^

…void…
^

int numbers…
^

System.out…
^

Not sure why you aren’t getting any answers when you write java PrimeDirective, as, assuming your still testing it with 6, you should be getting a return of false, but otherwise I’m happy I was able to at least stop part of the problem.

As for the errors, the only thing I can suggest is printing the entire code as is within the preformatted text option to the forum, and seeing if anyone else notices an issue. I’ll give it a look as well, but I can’t promise anything here, as that was the only issue I’d spotted before, and there’s obviously somehting more I missed.

A post was split to a new topic: Prime Directive Help

image
In step 7 it says set it up that if number is 2 make it return true.
Isn’t that a unnecessary step since we already accomplished that when we made the for() loop, for(int i = 2) that seems to return true for 2!? I tried it that way and its working fine.

image
This is what I did. I didn’t type if( number == 2){return true; } and it seems to work.

Could you please explain the reasoning and the logic behind adding “return true” after the “for” loop is finished? Thanks.

Hello, @course4520556896, and welcome to the forums.

I assume you are referring to code similar to this:

  public boolean isPrime(int number) {
    if (number == 2) {
      return true;
    } else if (number < 2 || number % 2 == 0) {
      return false;
    }
    
    for (int i = 3; i < number / 2; i += 2) {
      if (number % i == 0) {
        return false;
      }
    }
    return true;
  }

The for loop is iterating through all of the possible divisors that could go evenly into the number being tested which would reveal the number as not being prime. We use the if statement and modulo operator to determine whether the number can be divided evenly by the divisor. If it can, we return false because the number in question is not prime. Once we’ve tested every possible divisor, we know the number is prime, so we return true.

Remember that return hands a value back to the caller, and ends the execution of the method, so if execution of the code in the method hasn’t been terminated by the time the for loop has finished, the number must be prime, and true is returned.

2 Likes

I see, that makes sense. I was just wondering in this case why can’t you utilise the else statement as well. What I mean by that is why doesn’t it work to write else {return true;} or something similar to that because my initial thinking was to use else or else if within the for curly brackets.
Thanks for you help.

You don’t know a number is prime until you’ve tested all possible divisors. An else statement would return true prematurely.

Hello! I keep receiving the error missing return statement at line 20, I’ve googled and have been looking to see if the return type is wrong or something of the sort but I still can’t figure out what’s wrong.
I hope you can help me T.T (sorry if it’s a really simple/dumb question)

import java.util.ArrayList;

class PrimeDirective {

 

  // Add your methods here:

public String isPrime(int number){

  if(number== 2){

    return true;

  }else if(number<2){

      return false;

  }else {

  for(int i=2; i<number; i++){

    if((number/i) != 0){

      return true;

    }else{

      return false;

    }

    }

  } 

}

  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(6));

  }  

}

hallelujah! The compiler kept complaing of a missing } in the isPrime class. By making Class public PrimeDirective resolved my issue with the compiler failing.