Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Boolean value prints out the wrong one.

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Location
    Okahoma, US
    Posts
    18
    My Mood
    Grumpy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry Boolean value prints out the wrong one.

    I am making a text adventure for a simple project as a java beginner, and it caught my mind that no matter if the value is true or false, it always prints out as if it was true. I took the liberty of making a new project in eclipse and tested the theory by itself. It is still the same. Here is the code:

    public class BooleanLogic {
     
    	static boolean test = false;
     
    	public static void main(String[] args) {
    		if(test = true) {
    			System.out.println("true");
    			System.exit(0);
    		}
    		if(test = false) {
    			System.out.println("false");
    			System.exit(0);
    		}
    	}
    }

    its output is always true!


  2. #2
    Junior Member
    Join Date
    Feb 2013
    Location
    Germany
    Posts
    27
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Boolean value prints out the wrong one.

    you make an assignment:

    test = true means in java that you set the var test to true. And in java the assignment return its value so it allways true

    Give (test == true) a try.

    P.S.
    In pascal everything is easier, the math way anyway(insider joke)

    --- Update ---

    [/COLOR]and be carefuly with the system.excit

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Location
    Okahoma, US
    Posts
    18
    My Mood
    Grumpy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Boolean value prints out the wrong one.

    I only used System.exit so it would close after it printed out the value.

  4. #4
    Junior Member
    Join Date
    Feb 2013
    Location
    Germany
    Posts
    27
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Boolean value prints out the wrong one.

    Just a hint from a programmer, it's bad style to exit methods or return values in functions in more than one line. I didnt mean it in a bad way.

  5. #5
    Junior Member
    Join Date
    Dec 2012
    Location
    Okahoma, US
    Posts
    18
    My Mood
    Grumpy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Boolean value prints out the wrong one.

    Quote Originally Posted by janpiel View Post
    you make an assignment:

    test = true means in java that you set the var test to true. And in java the assignment return its value so it allways true

    Give (test == true) a try.

    P.S.
    In pascal everything is easier, the math way anyway(insider joke)

    --- Update ---

    [/COLOR]and be carefuly with the system.excit
    Fail... I haven't used java in a while, I won't tell you what I have been using since i may get banned :/

  6. #6
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default Re: Boolean value prints out the wrong one.

    First off '=' assigns a variable to the right operand of the equal sign, it does not test for equality. Use the equality operator == in your if statements.
    Second, you don't need two if-statements.
    Third, boolean variables can be used as true or false expressions.
    Fouth, it is bad programming practice to use those exits in if-statements. The code will naturally break off the if block.
    Fifth, you can actually print the variable itself for a true or false string.
     
    if(x)
        System.out.println(x);  //prints true if x is true
    else
        System.out.println(x); //prints false if x is false

Similar Threads

  1. Prints an extra character for this game.
    By TCDave in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 16th, 2012, 06:35 PM
  2. Replies: 9
    Last Post: August 30th, 2012, 04:25 PM
  3. [SOLVED] What's wrong with this boolean?
    By bgroenks96 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: June 8th, 2011, 07:41 PM
  4. nothing prints after runing code
    By darego in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 11th, 2011, 11:14 AM
  5. Boolean method returning a boolean value
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 21st, 2010, 10:56 AM

Tags for this Thread