Results 1 to 5 of 5
  1. #1
    ile4 is offline Member
    Join Date
    Nov 2010
    Posts
    24
    Rep Power
    0

    Default Checking if 2 things are false

    I am trying to implement a method which adds 2 classes, both of which have a field that stores their value as an int (e.g. 2) and a field which stores their sign (true if positive, false if negative).

    I want to add them, but at the moment it's ignoring their signs, so adding +2 and -3 is giving +5.

    I want an if statement to check if both class1 and class2 have false in their sign field.

    I have
    Java Code:
    if (this.sign = false && otherclass.sign = false)
    but it won't compile, saying 'unexpected type.' How can I check if they're both false, why is this wrong?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    1) Why not use a boolean for true/false? That's what it's for, and this way your code can be better understood by folks who read it?
    2) Hard to tell what your error is without seeing code.
    Do you have sign field?

    Also, it's not:
    Java Code:
    if (x = false) {
    ....
    Since = assigns the false value to x, it doesn't check for x being false.


    Better is:
    Java Code:
    if (x == false) {
    ....

    But the best true way to see if x is false is:
    Java Code:
    if (x) {
    ....
    If x is a boolean variable.

    If x is an int, and you want to check for < 0, then do that:
    Java Code:
    if (x < 0) {
    ....

    Anyway, still I recommend that you show the code and any error messages your code might generate.

    Luck!

  3. #3
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    Quote Originally Posted by Fubarable View Post

    But the best true way to see if x is false is:
    Java Code:
    if (x) {
    ....
    Sorry to be a pedant but that would be checking if x is true...
    you need to reverse it using the ! (not) symbol...
    Java Code:
    if (!x) {


    my first time correcting fubarable :cool:
    please dont kick me off the forum
    :D

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by berkeleybross View Post
    Sorry to be a pedant but that would be checking if x is true...
    you need to reverse it using the ! (not) symbol...
    Java Code:
    if (!x) {


    my first time correcting fubarable :cool:
    please dont kick me off the forum
    :D
    Thanks for the correction. Now get lost! (j/k).

  5. #5
    ile4 is offline Member
    Join Date
    Nov 2010
    Posts
    24
    Rep Power
    0

Similar Threads

  1. Move things in Applet
    By Chasingxsuns in forum New To Java
    Replies: 6
    Last Post: 02-19-2014, 09:21 AM
  2. Replies: 28
    Last Post: 09-15-2010, 11:21 AM
  3. XML, and other things.
    By Tortex in forum New To Java
    Replies: 5
    Last Post: 03-28-2010, 06:53 PM
  4. so whats going on? (things aren't showing up)
    By Adrien in forum AWT / Swing
    Replies: 9
    Last Post: 02-20-2010, 08:22 PM
  5. Program That Can Do Things On The Internet
    By IronLegion in forum Eclipse
    Replies: 1
    Last Post: 12-09-2009, 09:06 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
  •