• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Writing a static method that calculates whether the number in an array is a prime

 
Greenhorn
Posts: 25
1
Android
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

Screenshot-(104).png
[Thumbnail for Screenshot-(104).png]
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Andrea Menjivar
Greenhorn
Posts: 25
1
Android
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Henry Wong
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Andrea Menjivar
Greenhorn
Posts: 25
1
Android
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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;

 
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Think about this line of code.  The first prime number is 2, so it will evaluate to

What about elements primes[0] and primes[1]?  You don't want to use the number as the index into your array.
 
reply
    Bookmark Topic Watch Topic
  • New Topic