Results 1 to 6 of 6
  1. #1
    Tom_Snake is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

    Default 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:
    Java 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);
        }
    When I try to compile, it states "incompatible types" and leaves me befuddled. Isn't isPrime
    supposed to accept int as input? Please, explain to me what I'm doing wrong.
    Regards/ ts

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default 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.

  3. #3
    Tom_Snake is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

    Default 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?

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default 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

    Java Code:
    public boolean isPrime(int check){...}
    Then modify the code to return true or false in the correct position rather than printing stuff.

  5. #5
    Tom_Snake is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

    Default 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.

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Incompatible types - cannot call method to check int.

    You are welcome, I am glad to have helped.

Similar Threads

  1. incompatible types
    By effa in forum New To Java
    Replies: 16
    Last Post: 02-03-2011, 09:50 AM
  2. Incompatible types
    By bayan in forum New To Java
    Replies: 5
    Last Post: 11-04-2010, 08:43 AM
  3. incompatible types error
    By magic in forum New To Java
    Replies: 3
    Last Post: 06-02-2010, 04:58 PM
  4. Incompatible operand types int and double[][]
    By Haske2r in forum New To Java
    Replies: 2
    Last Post: 01-21-2010, 05:26 PM
  5. Incompatible types
    By coltragon in forum New To Java
    Replies: 5
    Last Post: 01-15-2010, 04:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •