Results 1 to 9 of 9
  1. #1
    shazakala is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default my simple boolean code keeps saying true

    This is just a simple boolean code to check if the number is between or equal 45 and 49, if it is then its true, if its not its false and it runs the lines in relation to those results. This is just a simple design where i put the number im wanting into the actual code just to save time. The issue im getting is that no matter what number i put in, it keeps thinking the statement is true. the program works, it's just not doing what i want it to. im pretty sure line 4 is at fault so could someone tell me how that line should be said? Oh and could you guys tell me how you put the code is so cleanly? all I can do is copy and paste. cheer's for any future help :)

    public class BoolanTest {
    public void start() {
    int mark = 3;
    boolean between = (mark <=49) && (mark >=45);
    if (between = true){
    System.out.println("is between or equal to 49 and 45");
    }
    if (between = false){
    System.out.println("Not between or equal to 49 and 45");
    }
    }
    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by shazakala View Post
    Java Code:
    		if (between = true){
    This assigns the value true to variable between and (of course) the value of the entire assignment expression will be true so the if-clause succeeds. Do this instead:

    Java Code:
    if (between)
       // between is true
    if (!between)
       // between is false
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    could you guys tell me how you put the code is so cleanly?
    Use code tags to post codes -- [code]CODE[/code] will display as
    Java Code:
    CODE
    Or paste your code between a pair of {code} tags.

    A single = sign is an assignment operator. It takes two of them to make up a conditional operator that tests for equality. More here.

    For booleans, there is no need to test for equality to true / false.
    Java Code:
    // if (between == true) {
    if (between) {
    }
    
    // if (between == false) {
    if (!between) {
    }
    db

    edit And who's the old sod today?

  4. #4
    shazakala is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default

    Thanks Darryl my code works now, yea == and = got mixed up i guess. At least I'm learning :) btw what are you talking about? "and who's the old sod today?"

    all help was appreciated

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

    Default

    I believe that may have been meant for jos. If you are done, please mark your thread solved with the thread tools at the top.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Darryl.Burke View Post
    edit And who's the old sod today?
    I really wouldn't know that; my guess is that it starts with a "D" and ends with "arryl Burke"; right?

    kind regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Jaipal is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by shazakala View Post
    This is just a simple boolean code to check if the number is between or equal 45 and 49, if it is then its true, if its not its false and it runs the lines in relation to those results. This is just a simple design where i put the number im wanting into the actual code just to save time. The issue im getting is that no matter what number i put in, it keeps thinking the statement is true. the program works, it's just not doing what i want it to. im pretty sure line 4 is at fault so could someone tell me how that line should be said? Oh and could you guys tell me how you put the code is so cleanly? all I can do is copy and paste. cheer's for any future help :)

    public class BoolanTest {
    public void start() {
    int mark = 3;
    boolean between = (mark <=49) && (mark >=45);
    if (between = true){
    System.out.println("is between or equal to 49 and 45");
    }
    if (between = false){
    System.out.println("Not between or equal to 49 and 45");
    }
    }
    }
    if(between=true)
    here jvm re assigning value true to variable between,so definately first if condittion will execute..
    run the following code u will get exact out put.....enjoy

    public class BoolanTest {
    public void start() {
    int mark = 3;
    if ((mark <=49) && (mark >=45)){
    System.out.println("is between or equal to 49 and 45");
    }
    else
    System.out.println("Not between or equal to 49 and 45");
    }
    public static void main(String args[]){
    new BoolanTest().start();
    }
    }

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    Quote Originally Posted by JosAH View Post
    I really wouldn't know that; my guess is that it starts with a "D" and ends with "arryl Burke"; right?

    kind regards,

    Jos ;-)
    That's been superseded by 'starts with J and ends with aipal ;)

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Darryl.Burke View Post
    That's been superseded by 'starts with J and ends with aipal ;)
    The BBC would like to apologize for the following remark; back to Luton: after the jury had left the room and was heading for the bar, a late contestant entered the stage; after furious protests from the entire audience the jury revoted and unanymously elected J-aipal the "Slowest Old Sod Of The Day". Ladies and gentlemen, we have a winner! Give him a big hand!

    kind regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Boolean always true?? What am I doing wrong?
    By Forty0ztoFreedom in forum New To Java
    Replies: 4
    Last Post: 03-02-2011, 07:13 PM
  2. Simple Boolean
    By jigglywiggly in forum New To Java
    Replies: 3
    Last Post: 01-01-2009, 06:01 AM
  3. Simple code. Could you help pls..?
    By Iliyas in forum New To Java
    Replies: 8
    Last Post: 12-26-2008, 04:17 AM
  4. Please help me this is simple bit of code
    By BlitzAcez in forum New To Java
    Replies: 4
    Last Post: 11-27-2008, 06:52 AM
  5. simple code
    By elizabeth in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:49 PM

Tags for this Thread

Posting Permissions

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