Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By gimbal2

Thread: How Do I Change True False Statements

  1. #1
    RSdroid is offline Member
    Join Date
    Dec 2013
    Posts
    3
    Rep Power
    0

    Default How Do I Change True False Statements

    What's up guys. I'm really new to Java and I'm using a self taught book (Java A Beginner's Guide). Basically here I created a truth table for Java's logical operators. My question is how do I get the program to display 1's and 0's rather than true and false? Anyone?

    Java Code:
    class LogicalOpTable {
      public static void main(String args[]) {
    
          boolean p, q;
    
          System.out.println("P\tQ\tAND\tOR\tXOR\tNOT");
    
          p = true; q = true;
          System.out.print(p + "\t" + q + "\t");
          System.out.print((p&g) + "\t" + (p|q) + "\t");
          System.out.println((p^q) + "\t" + (!p));
    
    
          p = true; q = false;
          System.out.print(p + "\t" + q + "\t");
          System.out.print((p&g) + "\t" + (p|q) + "\t");
          System.out.println((p^q) + "\t" + (!p));
    
    
          p = false; q = true;
          System.out.print(p + "\t" + q + "\t");
          System.out.print((p&g) + "\t" + (p|q) + "\t");
          System.out.println((p^q) + "\t" + (!p));
    
    
          p = false; q = false;
          System.out.print(p + "\t" + q + "\t");
          System.out.print((p&g) + "\t" + (p|q) + "\t");
          System.out.println((p^q) + "\t" + (!p));

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,987
    Rep Power
    9

    Default Re: How Do I Change True False Statements

    Sounds like a job for if statements or ternary operators.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,090
    Rep Power
    6

    Default Re: How Do I Change True False Statements

    Quote Originally Posted by KevinWorkman View Post
    Sounds like a job for if statements or ternary operators.
    or a static utility method that turns a boolean into an int. It all depends on what has already been learned so far. Another answer might be to not want to do this just yet, until more study has been performed.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    RSdroid is offline Member
    Join Date
    Dec 2013
    Posts
    3
    Rep Power
    0

    Default Re: How Do I Change True False Statements

    Quote Originally Posted by KevinWorkman View Post
    Sounds like a job for if statements or ternary operators.
    I know a little about if statements but nothing about ternary operators

    Quote Originally Posted by gimbal2 View Post
    or a static utility method that turns a boolean into an int. It all depends on what has already been learned so far. Another answer might be to not want to do this just yet, until more study has been performed.
    I tried to figure out myself how to turn a boolean into an int and don't seem to get the concept yet. I'm going to try and figure this out once more before giving up on this and probably rereading that chapter.
    Thanks to both!

  5. #5
    RSdroid is offline Member
    Join Date
    Dec 2013
    Posts
    3
    Rep Power
    0

    Default Re: How Do I Change True False Statements

    I seem to come to a stand still with the operator !
    This is what I'm doing.
    Java Code:
    class LogicalOpTable {
      public static void main(String args[]) {
     
          int p, q;
     
          System.out.println("P\tQ\tAND\tOR\tXOR\tNOT");
     
          p = 1; q = 1;
          System.out.print(p + "\t" + q + "\t");
          System.out.print((p&g) + "\t" + (p|q) + "\t");
          System.out.println((p^q) + "\t" + (!p));

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,090
    Rep Power
    6

    Default Re: How Do I Change True False Statements

    Yes, the not operator only works on a boolean and now you've turned it into integers. You can't do it like this.

    What you have here is a classic example of confusing data for the presentation of data. They are NOT the same. You want booleans in your code to do your operations on, but you want to PRINT an integer as the result.
    SurfMan likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Check a list , true false
    By schvuu in forum New To Java
    Replies: 1
    Last Post: 10-17-2013, 10:11 PM
  2. Boolean Arrays true/false switch
    By LetsG0Blue in forum New To Java
    Replies: 20
    Last Post: 03-20-2013, 11:14 AM
  3. how to balance true and false instances per id ?
    By aneuryzma in forum New To Java
    Replies: 1
    Last Post: 03-27-2011, 02:35 PM
  4. Prime Number - true , false
    By pinkdreammsss in forum Java Applets
    Replies: 11
    Last Post: 05-04-2010, 02:49 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
  •