PrimeDirective

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!
I am getting an error or no return statement on the isPrime method. I am not sure what I need to do. Any ideas? Thank you!

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:
// Import statement:
import java.util.ArrayList;
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;
} else {
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)); 

}

}

You could put a return true after the for loop (outside the loop) to guarantee that the function returns something.

Your code:

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;
    } else {
       return true;
    }
 }
};  // The ; is not needed here. 

Also, the return true should not be in the else block, because then it it would return true (meaning the number is prime) after checking only one possible factor [divisor].

So, just leave the else block blank?

Yes. You can leave out the else block.

This is the method I have for creating an array of only even or odd integers, but when I run it, it returns the arrays memory address instead of the array itself.
To implement the code in the main method:

System.out.println(pd.evenOdd(numbers,0));

ArrayError

public int[] evenOdd(int[] numbers, int choice) {
    if (choice == 0) {
      int count = 0;
      for (int num : numbers) {
        if (num % 2 == 0) {
          count++;
        }
      }
      int[] newArray = new int[count];
      int j = 0;
      for(int i = 0; i < numbers.length; i++) {
        if (numbers[i] % 2 == 0) {
          newArray[j] = numbers[i];
          j++;
        }
      }
      return newArray;
    }
    else if (choice == 1) {
      int count = 0;
    for (int num : numbers) {
        if (num % 2 != 0) {
          count++;
        }
      }
      int[] newArray = new int[count];
      int j = 0;
      for(int i = 0; i < numbers.length; i++) {
        if (numbers[i] % 2 != 0) {
          newArray[j] = numbers[i];
          j++;
        }
      }
      return newArray;
    }
    else {
      System.out.println("Invalid Choice");
    }
    return numbers;
  }

That’s normal in Java.
If you want to print the stuff in the array, you could iterate through the array,
and print each integer in the array, one at a time.

Or you could do
import java.util.Arrays;
and then do
System.out.println( Arrays.toString(pd.evenOdd(numbers,0)) );

Or you could make your own function to print the integer array.

  public static void printArray(int[] array) {
    int lastPos = array.length - 1;
    System.out.print("[");
    for (int i = 0; i < lastPos; i++) {
      System.out.print(array[i]);
      System.out.print(", ");
    }
    if (lastPos >= 0) {
      System.out.print(array[lastPos]);
    }
    System.out.println("]");
  }
1 Like