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: Printing i+1

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Printing i+1

    Hello,

    I have this part of source code

    for(int i=0;i<num;i++)
    {
    System.out.println("Recored number: " + i+1 + " is:");
    ....
    }

    Why the result of the println is "01" and not just "1" ?


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    571
    My Mood
    Sleepy
    Thanks
    9
    Thanked 84 Times in 78 Posts

    Default Re: Printing i+1

    Java treats that as a String concatenation. You need nested braces:
    System.out.println("Recored number: " + (i+1) + " is:");

  3. #3
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Printing i+1

    Hello.
    System.out.println() has a strange functionality.
    You should learn about it. Its regarding concatenating strings and adding numbers. We need to know which one is preferred over other in which scenarios.
    This link may be of use to you.
    concatenating string and numbers Java - Stack Overflow

    Syed.

  4. #4
    Junior Member
    Join Date
    Jul 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Printing i+1

    Thanks

Similar Threads

  1. printing
    By Gasper in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 16th, 2013, 04:57 AM
  2. Printing Using Swing.
    By LoganC in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 19th, 2012, 08:56 PM
  3. Printing from a Method
    By LoganC in forum What's Wrong With My Code?
    Replies: 11
    Last Post: October 4th, 2012, 06:33 PM
  4. Replies: 1
    Last Post: September 28th, 2011, 07:29 AM
  5. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 02:41 AM