Results 1 to 6 of 6
- 10-16-2011, 10:32 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 18
- Rep Power
- 0
Incompatible types - cannot call method to check int.
Hello!
I'm currently working on a prime number checker as well as an prime number adder, but i can't seem to get them to function properly.
I have written them in BlueJ, since I'm a newbie, and in different methods, in case I need the prime checker later on. What I want to do is check for all the primes below a value, in my case 10, and then sum these and print the value.
Here be some code:
When I try to compile, it states "incompatible types" and leaves me befuddled. Isn't isPrimeJava Code:/** * A basic brute-force prime checker. */ public void isPrime(int primeCheck){ int x = primeCheck; int y =2; while(x>y){ //while x>y works if((x%y) !=0){ y++; }else{ System.out.println("not a prime"); y=x; } } } /** * Basic add prime numbers, utilizing isPrime() */ public void primesBelow(){ int x=2; for(int i=3;i<10;i++){ if(isPrime(i)){ x+=i; } } System.out.println(x); }
supposed to accept int as input? Please, explain to me what I'm doing wrong.
Regards/ ts
- 10-16-2011, 10:36 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re: Incompatible types - cannot call method to check int.
That's not your problem. It does take an int. The problem is that if(...) expects a boolean (true or false), and isPrime returns void, not boolean.
- 10-16-2011, 10:42 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 18
- Rep Power
- 0
Re: Incompatible types - cannot call method to check int.
Oh, I see. Is this a matter of changing the return type of isPrime, or can I refrase the if in terms of if(isPrime== something clever) which evaluates to true and then get the expected result?
- 10-16-2011, 10:48 PM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re: Incompatible types - cannot call method to check int.
No, you cannot compare the return value of isPrime since it doesn't return anything but void. Regardless of whether the value passed in is prime or not, it still returns void.
Instead make the method return boolean
Then modify the code to return true or false in the correct position rather than printing stuff.Java Code:public boolean isPrime(int check){...}
- 10-16-2011, 10:56 PM #5
Member
- Join Date
- Oct 2011
- Posts
- 18
- Rep Power
- 0
Re: Incompatible types - cannot call method to check int.
Of course!
Needless to say, you've made me progress a little further in terms of understanding Java.
Thanks a lot.
- 10-16-2011, 10:57 PM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re: Incompatible types - cannot call method to check int.
You are welcome, I am glad to have helped.
Similar Threads
-
incompatible types
By effa in forum New To JavaReplies: 16Last Post: 02-03-2011, 09:50 AM -
Incompatible types
By bayan in forum New To JavaReplies: 5Last Post: 11-04-2010, 08:43 AM -
incompatible types error
By magic in forum New To JavaReplies: 3Last Post: 06-02-2010, 04:58 PM -
Incompatible operand types int and double[][]
By Haske2r in forum New To JavaReplies: 2Last Post: 01-21-2010, 05:26 PM -
Incompatible types
By coltragon in forum New To JavaReplies: 5Last Post: 01-15-2010, 04:47 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks