Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: incompatible types

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    6
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Question incompatible types

    hi, i was trying to do some simple stuff on java (i'm totally new at any kind of programming), and here, i got blocked, when i tried to compile my code (which would count to 500 if i am not mistaken), it said incompatible types (i use blueJ). here it is:

    public class q3
    {
        public static void main (String [] args)
        { int n=1;
            int i=0;
            int w=500;
            while (n>0)
            {
                i=i+n;
                if (i=w)
                {
                    break;
                }
            }
            System.out.print (i);
        }
     
    }

    I tried to put down just 500 instead of w, same response.

    Thanks for the answers, bye-bye
    Last edited by stylelink; July 10th, 2014 at 08:14 AM. Reason: making code easier to read


  2. #2
    Junior Member
    Join Date
    Jul 2014
    Posts
    28
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: incompatible types

    You would want to do this with a for loop, I think.

    for (int i = 0; i < 500; i++) {
    System.out.println(i);
    }

    Unless you're practicing while/if, in which case I'm not sure.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,765
    My Mood
    Mellow
    Thanks
    234
    Thanked 714 Times in 700 Posts

    Default Re: incompatible types

    Welcome to the Forum! Please read this topic to learn how to post code correctly and other useful tips for newcomers.

    Please post your code correctly per the above link.

    '=' is not a comparison operator. '==' is a comparison operator.

  4. The Following User Says Thank You to GregBrannon For This Useful Post:

    stylelink (July 10th, 2014)

  5. #4
    Junior Member
    Join Date
    Jul 2014
    Posts
    6
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: incompatible types

    well yes i was trying to practice the while/if, and also hoping for an explanation to why my types are incompatible, but thank you anyways

    --- Update ---

    and thank you greg, so what i should do is just put == instead of = and it will work right?

  6. #5
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    728
    Thanks
    0
    Thanked 132 Times in 115 Posts

    Default Re: incompatible types

    I just want to point out that:
    if (i=w)
    is not the same as:
    if (i==w)

    If you use a single equality sign then you assign a value to a variable.
    If you use two equality signs then you compare two values against each other.

  7. The Following User Says Thank You to Cornix For This Useful Post:

    stylelink (July 10th, 2014)

Similar Threads

  1. Incompatible types.
    By miller4103 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 12th, 2013, 07:36 PM
  2. Incompatible types
    By jadeclan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 26th, 2012, 06:09 AM
  3. Incompatible types
    By jadeclan in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 25th, 2012, 03:44 PM
  4. incompatible types!!
    By sneha343 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 30th, 2011, 04:48 PM
  5. incompatible types
    By frozen java in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 25th, 2011, 09:40 AM

Tags for this Thread