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 5 of 5

Thread: Concatenating booleans

  1. #1
    Member
    Join Date
    Jul 2013
    Location
    Mars
    Posts
    50
    My Mood
    Relaxed
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Concatenating booleans

    Hi guys,

    Having a hard time with what should be very simple assignment. Code below.

    class TaxReturn 
    {
    	public static void main(String[] args)
    	{
     
    	// declare the variables
    	int age = 36;
    	boolean hasDependants = true;
    	boolean jointReturn = true;
    	boolean categoryA = (!hasDependants && age <= 30) || (!jointReturn);
    	boolean categoryB = (hasDependants) || (age >= 45 && jointReturn);
    	boolean categoryC = (age >= 30 && age <= 45) && (!jointReturn || !hasDependants); 
     
    	System.out.println(categoryA + categoryB + categoryC);
     
     
     
     
    	} 
     
    }

    I have tried numerous things to try to get this to compile. When i put my boolean variables into separate print statements, it compiles just fine. When i try to ditch the '+' signs in an attempt to concatenate them together, and try using escape sequences instead it throws an error saying that I am missing semicolons, even though i am not. My desired output is this:
    'CategoryA' = "false"
    'CategoryB' = "true"
    'CategoryC' = "false"

    I know adding the single and double quotes require escape sequences, but those have also been somewhat problematic unfortunately, but that's another days question anyone who can help push me in the right direction towards understanding this gets a free dog online dog


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Concatenating booleans

    Sometimes you have to push the toString() logic to kick in by leading the objects in the print() method with quotes, even empty ones. Try this:

    System.out.println( "" + categoryA + categoryB + categoryC );

    And keep the danged dog. I already have one I can't get rid of.

  3. #3
    Member
    Join Date
    Jul 2013
    Location
    Mars
    Posts
    50
    My Mood
    Relaxed
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Concatenating booleans

    Perfect, that worked thank you. Now, as far as using the escape characters to add the single and multi quotes inside of the print statement. The assignment is to concatenate the output said above into one single print statement, otherwise i would just initialize a char or string variable with the value of (') and ("). I really know how to get this done, but apparently just not correctly. I was able to get single quotes inside of 'CategoryA' and tried the same thing to get double quotes around the value of categoryA, but that doesn't seem to work. Please let me know of i am just missing something obvious, because I'm getting quite frustrated. And p.s. fine if you do not want my dog, please accept my cow as a token of my appreciation . He's not good for much, but that's what you get for not taking the dog i guess

    class TaxReturn 
    {
    	public static void main(String[] args)
    	{
     
    	// declare the variables
    	int age = 36;
    	boolean hasDependants = true;
    	boolean jointReturn = true;
    	boolean categoryA = (!hasDependants && age <= 30) || (!jointReturn);
    	boolean categoryB = (hasDependants) || (age >= 45 && jointReturn);
    	boolean categoryC = (age >= 30 && age <= 45) && (!jointReturn || !hasDependants); 
     
     
    	System.out.println("\'CategoryA\'" + \"categoryA\" + categoryB + categoryC );
     
     
     
     
     
    	} 
     
    }

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Concatenating booleans

    I think you've made it harder than it really is. Include the items in quotes separate from those that aren't. Here's an example of how I would do one of them. The rest is up to you:

    System.out.println( "'CategoryA' = " + "\"" + categoryA + "\"" );

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    ///M Dood (September 15th, 2013)

  6. #5
    Member
    Join Date
    Jul 2013
    Location
    Mars
    Posts
    50
    My Mood
    Relaxed
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Concatenating booleans

    Awesome, exactly what i was looking for thank you very much! I must have gotten confused, i know that to assign a char variable with the value of a single quote you need to use escape characters. That must have been what was throwing me off. That and the fact that you need to concatenate the double quotes individually. I thought that \"example\" would work fine, shows what i know heh. Thanks again Greg i appreciate the help.

Similar Threads

  1. Methods, Booleans, and Initializing, OH MY!!
    By NickieBoren in forum Object Oriented Programming
    Replies: 6
    Last Post: August 22nd, 2013, 05:22 PM
  2. [SOLVED] Advice on booleans and yes/no inputs that deliver specific outputs
    By LeaAnn0223 in forum Loops & Control Statements
    Replies: 9
    Last Post: January 2nd, 2013, 09:44 PM
  3. Program is attempting to change booleans yet the result is unsuccesful.
    By CoolGuy134 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 26th, 2011, 08:48 AM
  4. Concatenating Inputs, need help please!
    By xdrechsler in forum File I/O & Other I/O Streams
    Replies: 17
    Last Post: August 19th, 2011, 01:02 PM
  5. Concatenating string with html tag
    By subhvi in forum Java Theory & Questions
    Replies: 4
    Last Post: September 29th, 2009, 05:47 AM