Results 1 to 5 of 5
  1. #1
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default Why does adding 1 to a int var within a println method treat as if they were string?

    Why does this...
    Java Code:
    System.out.println("Die " + i + 1 + ": " + dieArray[i]);
    Add the 1 to i as if it was strings? I tried (i+1) also and still same result.
    Java Code:
    Die 01: 4
    Die 11: 6
    Die 21: 6
    Die 31: 6
    Die 41: 4
    So I tried this and it worked....
    Java Code:
    System.out.printf("\nDie %d: %d", i+1, dieArray[i]);
    does this....
    Java Code:
    Die 1: 4
    Die 2: 5
    Die 3: 2
    Die 4: 1
    Die 5: 6

  2. #2
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Why does adding 1 to a int var within a println method treat as if they were stri

    Quote Originally Posted by AcousticBruce View Post
    I tried (i+1) also and still same result.
    This code should add the integer value of i to the integer value of 1 and print that value out:

    Java Code:
    System.out.println("Die " + (i + 1) + ": " + dieArray[i]);
    I think that you may have mistyped something when you tried it before. I recommend that you try it again.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Why does adding 1 to a int var within a println method treat as if they were stri

    Get in the habit of using standard Java naming conventions!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Why does adding 1 to a int var within a println method treat as if they were stri

    To answer your question, you need to dive into the Java Language Specification, or JLS, and in particular Section 5.4 on String conversion which states:
    String conversion applies only to an operand of the binary + operator which is not a String when the other operand is a String.

    In this single special case, the non-String operand to the + is converted to a String (5.1.11) and evaluation of the + operator proceeds as specified in 15.18.1.

  5. #5
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default Re: Why does adding 1 to a int var within a println method treat as if they were stri

    I understand fully on why it became a String. Thanks for the advice.
    Awinston you were correct. I did something wrong and am not sure what but I got it.

Similar Threads

  1. Why my cast has to be in println() method ?
    By fatabass in forum New To Java
    Replies: 6
    Last Post: 03-03-2012, 10:11 PM
  2. System.println(new String("Hi gang!"));
    By LinuxDev@Flanders in forum Introductions
    Replies: 6
    Last Post: 02-18-2012, 09:28 AM
  3. How does System.out.println() method work?
    By fatabass in forum New To Java
    Replies: 4
    Last Post: 01-17-2012, 11:04 AM
  4. Converting a println out in to a method
    By gcclinux in forum New To Java
    Replies: 9
    Last Post: 04-01-2011, 10:02 AM
  5. difference between system.out.println() & out.println()
    By wickedrahul9 in forum Advanced Java
    Replies: 5
    Last Post: 10-18-2008, 11:06 PM

Posting Permissions

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