Java problem please help

hi

im stuck on this challenge it says im missing return type but i’ve tried it so many ways but still keep getting an error, please help

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

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;

}

}

return;

}

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

}

}

What do you want this return to be returning:

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

if (number % i == 0){

return false;

} else {

return true;

}

}

return;
^^^^^^
}

Should this be of type bool as well?

In addition to what @codeneutrino said, see this code block below.

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

What would happen if we called pd.isPrime(9)? true would be returned even though 9 is not a prime number. Remember that after a return statement is executed, the method is exited. Your else statement essentially causes the loop to return false if the argument is even and true if the argument is odd.

3 Likes

hi

i’ve managed to get past the return issue and i think ive got the method isPrime in order, however now when i run the code i should see a display of all the prime numbers, but it only says true when i run this code not sure where im going wrong

import java.util.ArrayList;

class PrimeDirective {

// Add your methods here:

public boolean isPrime(int number){

if (number == 2){

return true;

} if (number < 2){

return false;

}

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

if (number % i == 0){

return false;

}

if (number % 3 == 0){

return false;    

}

}

return true;}

public Arraylist onlyPrimes(int numbers){

ArrayList primes = new ArrayList();

for (int number : numbers) {

if (isPrime(number)) {

  primes.add(number);

}

}

return 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(pd.onlyPrimes(numbers));

}

}

Please format your code using the </> button, it helps a lot with readability. It’s difficult to see exactly what’s going on. However, I do notice several things throughout your code, so I suggest that you look over it a few times yourself or that you review the previous lessons.

One way to find bugs in your code is to go step-by-step through it. So you would take your function call of pd.onlyPrimes(numbers) and follow the path it takes.

After reformatting and reviewing your code, don’t hesitate to post it back here where you can find more help!

1 Like

Hi dr_victoria

ive made some ground with the prime directive challenge (making a prime number list from an array of numbers putting them into a new ArrayList) , ive gotten down to it making a new ArrayList of primes, how ever it seems it has eliminated the non prime numbers except the number 33, i used a if then statement to eliminate this: else if (number % 3 == 0) { return false; } but im still seeing 33 when i print out the ArrayList, ill attach my code maybe you you’ll have some suggestions

on my isPrime method i was returning like this: return true && false; //but since i figured i only need true i took away false leaving only: return true; //but it seemed didnt make any difference as i think if then else statements will only execute on code that is true

when i run my code i get the result of: {29, 33, 11, 101, 43, 89}

heres my code

appreciate any feed back

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 if (number % 3 == 0) {

return false;

}

}

return true;

}

//new method for making list of primes

public ArrayList onlyPrimes(int numbers){

Arraylist primes = new ArrayList()

for (number : numbers) {

isPrime(number)

primes.add(number)

return 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(pd.onlyPrimes(numbers));

}

}

i am adding the to the ArrayList code but for some reason when i copy and pasted my code it didn’t copy that part

</> *** for some reason it doesnt work here either :slight_smile:

First things first, I ask that you go back and review everything you’ve learnt up until this point. There are many issues in your code.

Here are the things I’ve picked up on. I’m going to split this up into 2 replies. This reply highlights more minor bugs, while the next reply will discuss major issues with the logic of your code.

  • You’re missing several semicolons at the end of lines throughout your code.
  • The onlyPrimes method has a parameter that takes in an int, numbers. However, it should take in an int array, like int[] numbers.
  • You need to capitalize the L in ArrayList on the line Arraylist primes = newArrayList();.
  • You need to specify the type of number (in this case, int) on the line for (number : numbers).
  • You should specify the type of values that your ArrayList will hold since you know that primes will only hold int values (use <Integer> for this since <int> is not valid). More on that here.
  • Your return statement in your onlyPrimes method is nested inside the for loop. Remember that once a return statement is executed, the function is immediately exited. It’s vital to put your return statements where all the code you want to execute is executed before return.

Here are some instructions on how to format your code.

Code Blocks

Toolbar Icon
Using Preformatted Text icon on the toolbar

This will automatically create a code block which you can paste your code in.
capture_r
Or select your code and then press the icon.

1 Like
  • A point I forgot to mention on the previous reply was that you also need to declare String args in your main method as an array, like String[] args.
  • I also recommend you read these two articles on the static keyword and access modifiers.

This reply will go over more major issues in your code.

isPrime method

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

return true;

Your else if statement is checked every time number % i == 0 is false. This is completely unnecessary and redundant code since number % 3 == 0 will be checked on the second iteration of the loop.

onlyPrimes method

isPrime(number);
primes.add(number);

This first line simply calls the isPrime method and does nothing with the boolean value returned from it. The second line is executed for every number in numbers, meaning that every single number in the array passed to the method will be added to primes. What could you do to make use of the value returned from isPrime to ensure only prime numbers are added to primes?

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.onlyPrimes(numbers));
}

Why are you creating an instance of the PrimeDirective class here? You are already inside the class and have access to all its methods and variables. You can call the onlyPrimes method from the main method without creating an instance of the class.

1 Like

Hi dr_victoria once again thanks so much for your feedback

a lot of the errors you have pointed out seem to be elements of the code that the copy and paste missed out, sorry to have posted without checking

i have added and int to (int number : numbers) thanks for pointing this out

regarding the instance, it was already in the pre set code of this challenge when i started i did not write this code

i think this link here will give the code review correctly

im just going to look at the if else statements try get my head around what im doing wrong

Kind regards :slight_smile:

Yes, it looks a lot better! Just for readability purposes, any code inside a method should be indented to be under the corresponding method.


isPrime method
if (number == 2){
  return true;
} else if (number < 2){
  return false;
}

The above code inside your isPrime method works perfectly well.

} else if (number % 3 == 0) {
  return false;
} 

The reason why the above is unnecessary is because, if number % 2 == 0 is false (if it was true, false would be returned and the method would be exited), then number % 3 == 0 would be checked on the next iteration of the loop.


On line 26 (ArrayList<Integer> primes = new ArrayList<Integer>), remember that you still need the two parentheses (()) after you initialize primes to a new object of type ArrayList. This is done like so: ArrayList<Integer> primes = new ArrayList<Integer>();.


I’m not sure why the project already had a line creating an instance, but your code will work with or without it. You can access all of the methods inside your class directly since the main method is inside the same class. However, you can also still use an instance, in which case, your code inside the main method is correct.


You’re almost there! Nice job so far and I’m looking forward to seeing you finish this!

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.