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

Thread: Issue with output format

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Issue with output format

    Hello,

    I am a beginner in java and I want to output a line as follow :

    "your number is (x,y) and is on the third line "

    x and y are the user input and if I put a number for example 1 as x and 2 as y it add instead of putting a comma between the two numbers .

    my code is as follow :

    system.out.print("your number is " + (x + y)+" is on the third line ");

    I hope someone can help resolve this issue

    Thanks


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Issue with output format

    '2 + 3' is an int expression - it can be evaluated as 5. '"your number is " + 5' is evaluated as a string concatenation because the 5 is 'promoted' to a string:
    Conversions and Promotions
    See section 5.1.6
    Think of a way of writing your string concatenation so that there aren't two int values either side of a '+'.

  3. The Following User Says Thank You to Sean4u For This Useful Post:

    mael331 (September 25th, 2011)

  4. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Issue with output format

    Thanks so much Sean4u your links helped me a whole lot this what I came up with :

    System.out.printf ("("+x +","+ y+") is on the third line");

Similar Threads

  1. Format difference in System.out Vs. file output
    By mwr76 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 10th, 2011, 09:13 PM
  2. Format Java output
    By wildman0501 in forum Java Theory & Questions
    Replies: 2
    Last Post: June 11th, 2011, 06:09 PM
  3. Help with some code format
    By Highlord in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 9th, 2011, 09:27 PM
  4. Format Problems
    By maximus20895 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 27th, 2010, 09:48 PM
  5. How to format?
    By maximus20895 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 15th, 2010, 01:07 PM