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

Thread: problem in understanding output.

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default problem in understanding output.

    public class Function2
    {
    public static void main(String[] args)
    {
    sumAll(10,20,30);
    sumAll('a',10,10);
    }
    public static void sumAll(int a, int b, int c)
    {
    int sum = a+b+c;
    System.out.println(sum);
    }
    public static void sumAll(String a, int b, int c)
    {

    }
    }

    Output –
    60
    117
    Why 117, it should be a20. If it is correct, then when will it display a20.


  2. #2
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: problem in understanding output.

    In the second method, 'a' is a char, not a String. I am assuming that the compiler is getting it's ASCII value and then using the first sumAll() method.

  3. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem in understanding output.

    If I put "a" instead of 'a', will it display a20.

  4. #4
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: problem in understanding output.

    No it wouldn't since the second method doesn't do anything at all. There wouldn't be any result from it.

  5. #5
    Junior Member
    Join Date
    Jun 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem in understanding output.

    I want to display output a20.
    What should be the code then?

  6. #6
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: problem in understanding output.

    Create an empty String variable in the sumAll() method then concatenate a and the sum of b and c

  7. #7
    Junior Member
    Join Date
    Jun 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem in understanding output.

    string sum = a+b+c;

    where a is string and b,c are integers.

    Will it work then?

  8. #8
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: problem in understanding output.

    That would print out this: a1010

    Wrap b + c in parenthesis to force it to add them together then add it to the String.

  9. #9
    Junior Member
    Join Date
    Jun 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem in understanding output.

    I got it now.

    Thanks !

Similar Threads

  1. Problem understanding Java code modification
    By eagle09 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: June 26th, 2011, 04:13 PM
  2. problem with output
    By Timur in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: January 11th, 2011, 09:56 AM
  3. JTextArea output problem
    By grimx in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 8th, 2010, 07:54 PM
  4. Output problem (newbie)
    By Asido in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 8th, 2010, 12:19 PM
  5. xml output problem
    By tsili in forum Java Theory & Questions
    Replies: 4
    Last Post: May 25th, 2010, 04:56 AM