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

Thread: Very easy question about output.

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Very easy question about output.

    Hi! Iīm fairly new, and I havenīt figured out how to put both variables and text in the same place. ( sorry for my english ). Let me demonstrate.

    Letīs say I want command prompt to show a message "You have spent x euros and gained y profit." , where x and y are variables.

    the only way i know how to accomplish this is:

    System.out.printf("You have spent ");
    System.out.printf("%1$.2f", x);
    System.out.printf("euros and gained ")

    and so on. as you can see even though it works fine, itīs a very stupid way of doing this. I had something like this in mind:

    System.out.printf("%1$.2f", "You have spent", x, "euros and gained", y, "profit");

    but this way of writing it is incorrect. Can someone please show me the correct way of doing this?
    Help is much appreciated


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Very easy question about output.

    Quote Originally Posted by ma5sacre View Post
    System.out.printf("You have spent ");
    System.out.printf("%1$.2f", x);
    System.out.printf("euros and gained ")
    System.out.printf("You have spent %1$.2f euros and gained ", x);
    And if you want a trailing newline, put %n after gained (before " ).
    Andrea, www.andbin.net — SCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginners – My new project Java Examples on Google Code

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,809
    My Mood
    Mellow
    Thanks
    177
    Thanked 626 Times in 613 Posts

    Default Re: Very easy question about output.

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for newcomers.

    Stings can be concatenated (or added) together and included in the same print statement, as in:

    System.out.println( "This" + " that " + " and the other thing." );

    will send the combined arguments of the println() method to the standard output, probably the console or command line.

    The printf() method can use that same approach, but is also slightly different. (You can read more about the printf() method in the API documentation.) The code you posted can be combined to:

    System.out.printf("You have spent %1$.2f euros and gained ", x );

    and so on . . .

  4. #4
    Junior Member
    Join Date
    Jan 2014
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Very easy question about output.

    Thanks for your replies! It works now!

Similar Threads

  1. An easy question
    By KAJLogic in forum Java Theory & Questions
    Replies: 5
    Last Post: December 1st, 2013, 03:55 AM
  2. [SOLVED] Absolute Beginner Here, should be an easy question for anyone really.
    By Harrald in forum What's Wrong With My Code?
    Replies: 27
    Last Post: July 31st, 2012, 09:40 PM
  3. How do I get this to work? Easy question, I'm just hopeless at it!
    By akmi5 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 1st, 2012, 01:31 PM
  4. Easy array question
    By surfbumb in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 3rd, 2011, 08:44 PM
  5. Quick easy Java question.
    By DHG in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 25th, 2011, 02:59 PM

Tags for this Thread