Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By JosAH

Thread: I want to print two variables in same System.out.print, but not as one sum

  1. #1
    LasseA is offline Member
    Join Date
    Oct 2012
    Posts
    29
    Rep Power
    0

    Default I want to print two variables in same System.out.print, but not as one sum

    I want to the outprint 1122334455667788990011223344556677889900
    I could of course just make two System.out.print statements, but I'm pretty sure there should be a way so I can keep it in one, without the two "k"'s becoming one number, instead of two seperate.

    Java Code:
    for (int j = 1 ; j <= 2 ; j++) {
    			for ( int k = 1 ; k <= 9 ; k++) {
    				System.out.print(k+k);
    			}
    			System.out.print("00");
    		}
    I guess it's a pretty basic thing, but I can't seem to find it in my Java book.

  2. #2
    LasseA is offline Member
    Join Date
    Oct 2012
    Posts
    29
    Rep Power
    0

    Default Re: I want to print two variables in same System.out.print, but not as one sum

    Okay I got a way (k+""+k) does the job, but if there is any more simple way I would still like to hear :)

  3. #3
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    4

    Default Re: I want to print two variables in same System.out.print, but not as one sum

    Actually that is the way it goes - maybe a little less magic would be:
    System.out.print( k.toString() + k.toString() ); // EDIT: must be valueOf here...

    That is basically what is also happening when you do it your way. There are also other ways such as:
    System.out.printf( "%d%d", k, k );
    Last edited by Sierra; 01-07-2013 at 03:24 PM.
    I like likes!

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,361
    Rep Power
    5

    Default Re: I want to print two variables in same System.out.print, but not as one sum

    That's about as simple as it gets. System.out.println is overloaded with all the primitives, String and Object. k+k is evaluated before the println executes so k+k gets summed. k + "" + k is evaluated to a String, although if you do ""+k+k it will still be summed.

    Edit: k.toString() does not work on an int. String.valueOf(k) would be more appropriate, but in the end it doesn't really matter as long as you get the job done :)
    Last edited by SurfMan; 01-07-2013 at 03:05 PM.

  5. #5
    LasseA is offline Member
    Join Date
    Oct 2012
    Posts
    29
    Rep Power
    0

    Default Re: I want to print two variables in same System.out.print, but not as one sum

    Thanks man! just the kind of answers I was looking for :)

  6. #6
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    4

    Default Re: I want to print two variables in same System.out.print, but not as one sum

    Quote Originally Posted by SurfMan View Post
    Edit: k.toString() does not work on an int. String.valueOf(k) would be more appropriate, but in the end it doesn't really matter as long as you get the job done :)
    Sorry, yes it is a primitive... I should try before I post... it must be
    System.out.print( String.valueOf(k) + String.valueOf(k) );
    though I like ""+k+k better anyway. ;)
    I like likes!

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,190
    Blog Entries
    7
    Rep Power
    24

    Default Re: I want to print two variables in same System.out.print, but not as one sum

    Because you want to print a digit in the range [1 ... 9] two times, why not do: System.out.print(11*k) ...

    kind regards,

    Jos
    Sierra likes this.
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Javax Print Attribute for Selection Print Range
    By rsawatzky in forum AWT / Swing
    Replies: 0
    Last Post: 04-26-2012, 12:14 AM
  2. Replies: 8
    Last Post: 06-21-2011, 02:21 PM
  3. Loop System print or println statements
    By Singing Boyo in forum New To Java
    Replies: 6
    Last Post: 03-22-2009, 04:52 AM
  4. system-print(ln) on jtextarea or jpanel
    By Tamu in forum Advanced Java
    Replies: 1
    Last Post: 11-25-2008, 06:18 PM
  5. Print the text file and print preview them
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:04 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
  •