Results 1 to 7 of 7
  1. #1
    rahil.khan is offline Member
    Join Date
    Oct 2014
    Location
    NEW DELHI
    Posts
    41
    Rep Power
    0

    Post How to add in Println

    class first
    {
    public static void main(String []args)
    {
    System.out.println(args[0]+" "+args[1]+" "+args[3]+" "+args[4]);
    int dec = 267;
    int hex = 0x10B;
    int oct = 0413;
    int bin = 0b100001011;
    System.out.println("Octal + Binary : "+oct+bin);
    System.out.println("HexaDecimal + Decimal : "+dec+hex);
    }
    }

    i want add two variables in print method.... how should i use the concatenate operator for the same.
    Last edited by rahil.khan; 10-10-2014 at 03:22 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: How to add in Println

    BB Code List - Java Programming Forum - Learn Java Programming

    Use parentheses.
    Java Code:
    System.out.println("Octal + Binary : " + (oct + bin));
    Note that this is not specific to println but rather the way the + operator works for String concatenation.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    rahil.khan is offline Member
    Join Date
    Oct 2014
    Location
    NEW DELHI
    Posts
    41
    Rep Power
    0

    Default Re: How to add in Println

    so what's the correct way?

    thnx....

    is there any other way to do it i mean without the parentheses???
    Last edited by rahil.khan; 10-10-2014 at 03:36 PM.

  4. #4
    rahil.khan is offline Member
    Join Date
    Oct 2014
    Location
    NEW DELHI
    Posts
    41
    Rep Power
    0

    Default Re: How to add in Println

    so what's the correct way?

    thnx....

    is there any other way to do it i mean without the parentheses???

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How to add in Println

    Quote Originally Posted by rahil.khan View Post
    is there any other way to do it i mean without the parentheses???
    Yes. Compute the sum outside of the print statement.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Nybbler is offline Member
    Join Date
    Feb 2014
    Posts
    7
    Rep Power
    0

    Default Re: How to add in Println

    You could use System.out.printf instead. But you'd have to learn how to construct the format string.

    System.out.printf("Octal + Binary : %d\n ", oct+bin);
    System.out.printf("HexaDecimal + Decimal : %d\n ", dec+hex);

    The '%d' means you want an int type in the given location. Then you give the value in the corresponding parameter. The '\n' in the string means "newline" which will put the cursor at the next line.

    A quick overview of printf
    Last edited by Nybbler; 10-10-2014 at 06:42 PM.

  7. #7
    rahil.khan is offline Member
    Join Date
    Oct 2014
    Location
    NEW DELHI
    Posts
    41
    Rep Power
    0

    Default Re: How to add in Println

    thnx alot... this forum helps alot... great help..

Similar Threads

  1. System.out.println
    By JohnDoe in forum New To Java
    Replies: 1
    Last Post: 09-05-2010, 11:14 AM
  2. System.out.println(040|343);
    By satheshshiva in forum New To Java
    Replies: 5
    Last Post: 06-26-2010, 11:26 PM
  3. Println VS system.out.println
    By ccie007 in forum New To Java
    Replies: 2
    Last Post: 05-20-2010, 09:52 AM
  4. Need help with println
    By jhetfield18 in forum New To Java
    Replies: 8
    Last Post: 09-18-2009, 09:26 AM
  5. difference between system.out.println() & out.println()
    By wickedrahul9 in forum Advanced Java
    Replies: 5
    Last Post: 10-19-2008, 12:06 AM

Posting Permissions

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