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

Thread: How print " in output?

  1. #1
    Member
    Join Date
    Sep 2012
    Location
    The Netherlands
    Posts
    84
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Question How print " in output?

    Does anyone know how to print the "in an output line?
    Like I want my output link to be: name = "Purple"


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,172
    My Mood
    Hungover
    Thanks
    141
    Thanked 599 Times in 514 Posts

    Default Re: How print " in output?

    Use an escape character. Like this:
    "\"Purple\""

    Recommended reading: Characters (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Sep 2012
    Location
    The Netherlands
    Posts
    84
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: How print " in output?

    Thanks for that.
    Then the next question, how do I print a value behind it.
    Like:

    String Chr = Y;
     
    "\"Purple" + Chr"\"

    "\"Purple" + Chr"\" --> doesn't work.
    I would like as output "PurpleY"
    Last edited by Purple01; November 27th, 2012 at 06:49 AM. Reason: Editiong capital letters

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: How print " in output?

    Quote Originally Posted by Purple01 View Post
    Thanks for that.
    Then the next question, how do I print a value behind it.
    Like:

    String Chr = Y;
     
    "\"Purple" + chr"\"

    "\"Purple" + chr"\" --> doesn't work.
    I would like as output "PurpleY"
    What's Y?
    What chr? Because Chr != chr.
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford

  5. #5
    Member
    Join Date
    Sep 2012
    Location
    The Netherlands
    Posts
    84
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: How print " in output?

    I will mind the capital letter next time yes.
    Chr and chr are the same, I will adjust that in my post.

  6. #6
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: How print " in output?

    Quote Originally Posted by Purple01 View Post
    I will mind the capital letter next time yes.
    Chr and chr are the same, I will adjust that in my post.
    You still didn't answer my other question. What is Y?
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford

  7. #7
    Member
    Join Date
    Sep 2012
    Location
    The Netherlands
    Posts
    84
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: How print " in output?

    Quote Originally Posted by Mr.777 View Post
    You still didn't answer my other question. What is Y?
    That is a letter I pulled out of an arrayList.

    for (int x = 0; x < arrayList_Chrom.size(); x++) {
    			String Chr = arrayList_Chrom.get(x);
    }

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,172
    My Mood
    Hungover
    Thanks
    141
    Thanked 599 Times in 514 Posts

    Default Re: How print " in output?

    You have to remember that "\"" is a String, so you do String concatenation exactly like you do with any other String. For example, consider this:

    String chr = "Y";
    "Purple" + chr + "."
    //PurpleY.

    Also consider using a StringBuilder instead of String concatenation.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Purple01 (November 27th, 2012)

  10. #9
    Member
    Join Date
    Sep 2012
    Location
    The Netherlands
    Posts
    84
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: How print " in output?

    Thanks for letting me know that.
    I also noticed that by splitting .. you also need those \\ things before it.

  11. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,172
    My Mood
    Hungover
    Thanks
    141
    Thanked 599 Times in 514 Posts

    Default Re: How print " in output?

    Quote Originally Posted by Purple01 View Post
    Thanks for letting me know that.
    I also noticed that by splitting .. you also need those \\ things before it.
    I'm not sure exactly what you mean, but yeah, anytime you have a quotation mark inside a String, you need to escape it. Doesn't matter if the String is split up into multiple String primitives or not.

    Unless you're talking about the String.split() function, which takes a regular expression String, so you have to escape the escape sequence...
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Why is the output always "0"?
    By TheBattousaixx in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 24th, 2012, 09:39 PM
  2. Replies: 3
    Last Post: December 7th, 2011, 01:03 AM
  3. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  4. [SOLVED] how to print for-loop output to JTextArea
    By voltaire in forum AWT / Java Swing
    Replies: 2
    Last Post: May 13th, 2010, 02:43 PM
  5. [SOLVED] why does this code print 0.0 for the value and "F" as the letter grade?
    By etidd in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 12th, 2010, 09:38 PM