Results 1 to 4 of 4
  1. #1
    jadroe is offline Member
    Join Date
    Mar 2011
    Posts
    1
    Rep Power
    0

    Default Quotes around variables

    How can I put quotes, using the escape sequence \'\' or \"\", around variables?
    For example:

    System.out.print("You have entered " + num + ".");

    I want the output to look like this: You have entered "num goes here". Can this even be done?

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Yes, it can be done, with Java formatter.

    If the number is an integer, use %d e.g.

    Java Code:
    //note we use 'printf' to use the Java formatter
    System.out.printf("You have entered %d.",num);

    Please read here for more info:
    Formatting Numeric Print Output (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)
    Last edited by ozzyman; 03-16-2011 at 09:35 PM.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I don't see why you would want a formatted, he wants quotes around his number and that's all, no formatting needed.

    @op: use the escaped quote.
    Java Code:
    String s = "hi the number is\"" + num + "\".";
    Last edited by sunde887; 03-16-2011 at 09:42 PM.

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Oh right, I just read:
    For example:
    System.out.print("You have entered " + num + ".");

Similar Threads

  1. String, quotes and args
    By mac in forum New To Java
    Replies: 8
    Last Post: 02-04-2010, 04:24 PM
  2. unbalanced quotes in -
    By fresh83 in forum New To Java
    Replies: 2
    Last Post: 12-19-2009, 01:10 AM
  3. Stock Quotes
    By BartDR in forum Advanced Java
    Replies: 0
    Last Post: 09-15-2009, 09:15 AM
  4. The Fav. Programming Quotes Thread
    By angryboy in forum Forum Lobby
    Replies: 3
    Last Post: 06-13-2009, 10:16 AM
  5. Inserting data containing quotes into DB
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-06-2008, 09:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •