Results 1 to 4 of 4
  1. #1
    PeterFeng is offline Member
    Join Date
    Jan 2009
    Posts
    22
    Rep Power
    0

    Default WHy does it say imcompatable types?

    //Temp mem variable
    int tempA1=0, tempA2=0,tempA3=0, tempA4=0, tempA5=0;

    if(tempA1=tempA2=tempA3=tempA4=tempA5=0)
    {
    eFrag1=memAc1;
    }


    why does it say imcomptable types when tempa1-5 are all int?

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    inside if statement, variable must be boolean,

    tempA1=tempA2=tempA3=tempA4=tempA5=0
    variable assign only and is int type

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default "and" or "or"

    Peter: What are you trying to do? Using an "if" statement means a decision. Do you need to check if only one the variables is equal to zero or do you need to check if all the variables are equal to zero?
    • To compare amounts use "=="
    • If you want to check if only one variable is zero, then use the "and" operator: &&
    • If you want to check if all variables are zero, then use the "or" operator: ||

    A useful link:
    Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  4. #4
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    MT and CJ are correct in what they are saying.

    Your statement assigned 0 to temp5, temp5 to temp4, and so on. At the end of all that, I *think* the result was the value of temp1 (0), and if() expects a boolean. Therefore, you ended up with a type exception.

    You use == to compare values, which returns a boolean. You use && to "add" two boolean values and || to "or" them. Your statement should look like
    Java Code:
    if (temp1 == 0 && temp2 == 0 && ...) {
      // do something
    }

Similar Threads

  1. Array of different data types?
    By venkatteshb in forum New To Java
    Replies: 1
    Last Post: 08-27-2008, 05:42 PM
  2. Hastable - Multiple Types
    By sopna sajith in forum Advanced Java
    Replies: 3
    Last Post: 06-29-2008, 04:40 AM
  3. Converting data types
    By bluekswing in forum New To Java
    Replies: 4
    Last Post: 01-12-2008, 12:48 AM

Posting Permissions

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