posted 1 year ago

Hi all,

I seem to be having a hard time understanding how to do this. The assignment requires us to " Write a method isPrime that determines if a number is prime. If the number is prime the method returns true, otherwise it returns false". Then in main, we have to create an array and call the method to find each prime number and store it in the array. This is what I wrote so far, but I am getting this error (will post as an attachment). How would I use the method to determine if the number is prime and then store that number in the array?

I seem to be having a hard time understanding how to do this. The assignment requires us to " Write a method isPrime that determines if a number is prime. If the number is prime the method returns true, otherwise it returns false". Then in main, we have to create an array and call the method to find each prime number and store it in the array. This is what I wrote so far, but I am getting this error (will post as an attachment). How would I use the method to determine if the number is prime and then store that number in the array?

Screenshot-(104).png

posted 1 year ago

The isPrime() method takes a long value, and returns a boolean value. Your are trying to pass in a long array instance, and trying to assign the result to a long array reference variable.

Neither operation is allowed, as (1) a long array is *not* a long, and (2) a boolean is *not* a long array.

Henry

- 1

The isPrime() method takes a long value, and returns a boolean value. Your are trying to pass in a long array instance, and trying to assign the result to a long array reference variable.

Neither operation is allowed, as (1) a long array is *not* a long, and (2) a boolean is *not* a long array.

Henry

posted 1 year ago

Which is exactly what confuses me. But the instructions in my assignment say, " Write a method isPrime that determines if a number is prime. If the number is prime the method returns true, otherwise it returns false".

Unless I am misinterpreting that, I would love if someone could help clarify this for me?

Henry Wong wrote:

The isPrime() method takes a long value, and returns a boolean value. Your are trying to pass in a long array instance, and trying to assign the result to a long array reference variable.

Neither operation is allowed, as (1) a long array is *not* a long, and (2) a boolean is *not* a long array.

Henry

Which is exactly what confuses me. But the instructions in my assignment say, " Write a method isPrime that determines if a number is prime. If the number is prime the method returns true, otherwise it returns false".

Unless I am misinterpreting that, I would love if someone could help clarify this for me?

posted 1 year ago

First, I didn't dive into your code, so can't tell you that it works correctly or not ... However, it does look like you did what the assignment said. The method does take a number. And the method also returns either true or false. This is not the problem.

The problem (which the compiler is complaining about) is how you use the method that you implemented. You did not give the method a number -- you gave the method an array. You don't expect a true or false to be returned -- you expect an array to be returned.

Henry

- 1

Andrea Menjivar wrote:

Which is exactly what confuses me. But the instructions in my assignment say, " Write a method isPrime that determines if a number is prime. If the number is prime the method returns true, otherwise it returns false".

Unless I am misinterpreting that, I would love if someone could help clarify this for me?

First, I didn't dive into your code, so can't tell you that it works correctly or not ... However, it does look like you did what the assignment said. The method does take a number. And the method also returns either true or false. This is not the problem.

The problem (which the compiler is complaining about) is how you use the method that you implemented. You did not give the method a number -- you gave the method an array. You don't expect a true or false to be returned -- you expect an array to be returned.

Henry

posted 1 year ago

Hi, so I rewrote my code but I am still confused as to how to place the prime numbers in the array. I am getting the error "incompatible types: possible lossy conversion from long to int" for the line

primes[number] = number;

Henry Wong wrote:Andrea Menjivar wrote:

Which is exactly what confuses me. But the instructions in my assignment say, " Write a method isPrime that determines if a number is prime. If the number is prime the method returns true, otherwise it returns false".

Unless I am misinterpreting that, I would love if someone could help clarify this for me?

First, I didn't dive into your code, so can't tell you that it works correctly or not ... However, it does look like you did what the assignment said. The method does take a number. And the method also returns either true or false. This is not the problem.

The problem (which the compiler is complaining about) is how you use the method that you implemented. You did not give the method a number -- you gave the method an array. You don't expect a true or false to be returned -- you expect an array to be returned.

Henry

Hi, so I rewrote my code but I am still confused as to how to place the prime numbers in the array. I am getting the error "incompatible types: possible lossy conversion from long to int" for the line

primes[number] = number;