Results 1 to 6 of 6
  1. #1
    innovo is offline Member
    Join Date
    Jul 2015
    Posts
    84
    Rep Power
    0

    Default how to use Boolean Datatype to tell if condition is true or not?

    Hello all, Currently I have a boolean datatype and I am trying to use it to indicate a condition in my code.

    I am trying to say if numberinput is greater than 1 anded progress =1 is true then do something. Line number 12 comes out as an error for me
    Java Code:
    nextButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
     int numberinput = Integer.parseInt(jTextField2.getText());
     boolean progressTorF;
     progressTorF = true;
     
            writeDatas();
            
            if(progress > dataRange){
                writeOutFile();
                System.exit(0);
            }
            if(numberinput>1 && progress=1){
                
                
            }
    All I know how to do is say boolean = true or false. How can I go about using boolean for a condition?

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: how to use Boolean Datatype to tell if condition is true or not?

    To compare with a boolean result, use ==, not =

    Edit: reworded
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: how to use Boolean Datatype to tell if condition is true or not?

    comes out as an error
    It helps us a lot if you copy and post here the full text of the error message.

    It's possible to define a variable and give it an initial value in one statement (vs two):
    Java Code:
       boolean progressTorF = true;
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: how to use Boolean Datatype to tell if condition is true or not?

    Quote Originally Posted by innovo View Post
    How can I go about using boolean for a condition?
    Quote Originally Posted by SurfMan View Post
    To compare with a boolean result, use ==, not =
    No no no no NO!

    Just use the boolean.
    Java Code:
    if (progressTorF) {
      ...
    }
    Terrible variable name though; depending on the application I would suggest hasProgress, progressed or hasProgressed.

    Oh, and if you think you really should compare a boolean variable with a boolean literal, why stop at one?
    Java Code:
    if ((((progressTorF == true) == true) == true) == true) {
    @innovo: for testing that a boolean variable is false, use the negation operator:
    Java Code:
    if (!progressTorF) {
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: how to use Boolean Datatype to tell if condition is true or not?

    Quote Originally Posted by DarrylBurke View Post
    No no no no NO!

    Just use the boolean.
    Ofcourse, if the variable you're using is a boolean, go ahead. OP was using something like an int, comparing it to 1. Writing if(progress == 1) would be very valid in that case. That's what I was aiming at.

    If you see me writing
    Java Code:
    boolean theSuxzorz = true;
    if ( theSuxzorz == true) { ... }
    then I'll hand in my Java Programmer card and see myself out the building.
    Last edited by SurfMan; 09-01-2015 at 03:24 PM.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: how to use Boolean Datatype to tell if condition is true or not?

    For status checking I prefer to do something like the following:

    Java Code:
    enum State { RUNNING, PAUSED, STOPPED;
       private boolean state = false;
       
       public boolean isSet() {
          return state;
       }
       
       public void set() {
          state = true;
       }
       
       public void clear() {
          state = false;
       }
      
    }
    public class StatusDemo {
      public static void main(String[] args) {
          
          setRunning();
          if (isRunning()) {
             System.out.println("Now running!");
          }
       }
    
       public static boolean isRunning() {
          return State.RUNNING.isSet();
       }
       
       public static void setRunning() {
          State.RUNNING.set();
       }
       
       public static void clearRunning() {
          State.RUNNING.clear();
       }
    }
    The helper routines remove having to deal with the actual enums. Instead of using a boolean in the enum, you
    can employ an EnumSet. If the set contains the value then it is considered set, otherwise, clear.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Infinite loop using boolean as condition
    By midnightcoffee in forum New To Java
    Replies: 2
    Last Post: 11-14-2013, 11:50 PM
  2. Accessing specific class if condition is true
    By Hash in forum New To Java
    Replies: 1
    Last Post: 02-24-2012, 03:43 AM
  3. Replies: 3
    Last Post: 09-13-2011, 12:01 AM
  4. boolean condition creating problem
    By Basit781 in forum CLDC and MIDP
    Replies: 1
    Last Post: 05-25-2010, 07:57 AM
  5. How do I stop or kill a running thread when a condition is true
    By chikaman in forum Threads and Synchronization
    Replies: 3
    Last Post: 12-21-2009, 11:07 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
  •