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

Thread: Pls help out with this code...What is wrong with it?

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Pls help out with this code...What is wrong with it?

    The code looks straightforward except that it is not working the way i expected it to.


    Your assistance will be gladly appreciated.

    Thanks.
    =========================

    package sumaverage;

    public class SumAverage {

    public static void main(String args[]) {
    int sum = 0;
    String args.length[] = new String[10];
    int i;
    float Average;
    for(i= 0; i<=args.length; i++){
    sum = sum+ args.length;
    }
    System.out.println("The sum is:" +sum);
    Average = sum/args.length;
    System.out.println("The Average is:" +Average);

    }

    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Pls help out with this code...What is wrong with it?

    Welcome to the Forum! Please read this topic to learn how to post code correctly and other useful tips for newcomers.

    Please post your code correctly per the above link.
    . . . it is not working the way i expected it to.
    Please describe what you expected and how the code executes differently than you expected. An excellent way to accomplish that is to post a sample run so that we can see how to duplicate the bad behavior and then describe how the sample run would be different if the behavior was correct.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Pls help out with this code...What is wrong with it?

    Thread moved to "What's wrong.." section.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Dec 2013
    Location
    London
    Posts
    11
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Pls help out with this code...What is wrong with it?

    This code will not work:
    1. args is already declared system variable for receiving string array from the system, therefore compiler will give an error
    2. args.length - is not acceptable name for variable, as dot is used as operator, I assume either you wanted to create new independent variable or your code wanted to check what system receiving values are (both are incorrectly implemented, however)
    Assume this:
    package sumaverage;

    public class SumAverage {

    public static void main(String args[]) {
    int sum = 0;
    String arg[] = new String[10];
    int i;
    float Average;
    for(i= 0; i<=arg.length; i++){
    sum = sum+ arg.length;
    }
    System.out.println("The sum is:" +sum);
    Average = sum/arg.length;
    System.out.println("The Average is:" +Average);

    }

    }

  5. #5
    Junior Member
    Join Date
    Sep 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Pls help out with this code...What is wrong with it?

    Thanks alot guys
    Pls skuch89, it ran well but it was still displaying this error:

    "run:

    **The sum is:110
    Exception in thread "main" java.lang.ArithmeticException: / by zero
    at sumaverage.SumAverage.main(SumAverage.java:16)**

    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)"

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Pls help out with this code...What is wrong with it?

    Again, we'd be glad to help you if we knew what your code was supposed to do. Code comments would be great, but if you're unable to bring yourself to comment your own code, provide a statement of the code's purpose, the assignment's instructions, *something* that tells us what the code is supposed to do.

  7. #7
    Junior Member
    Join Date
    Sep 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Pls help out with this code...What is wrong with it?

    Thanks alot GregBranno, the code was supposed to accept some numbers and add them up and find the average. Thanks

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Pls help out with this code...What is wrong with it?

    Exception in thread "main" java.lang.ArithmeticException: / by zero
    at sumaverage.SumAverage.main(SumAverage.java:16)
    At line 16 the program tries to divide by zero which is not allowed. The code should check for a zero valued divisor and print a message instead of trying to do the division. Look at an if / else statement for that.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Dec 2013
    Location
    London
    Posts
    11
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Pls help out with this code...What is wrong with it?

    Quote Originally Posted by babsgenius2001 View Post
    Thanks alot guys
    Pls skuch89, it ran well but it was still displaying this error:

    "run:

    **The sum is:110
    Exception in thread "main" java.lang.ArithmeticException: / by zero
    at sumaverage.SumAverage.main(SumAverage.java:16)**

    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)"
    babsqenius2001, ermm your exception in code is actually showing that you try to divide number by 0, which obviously not allowed. But I checked my written code, it is correct running. I assume you made mistake whenever typed in code (maybe copy paste instead, especially check in debugging mode "Average = sum/arg.length;".
    Here is a screenshot of my running code:

    [SCREENSHOT REMOVED]

    Secondly, if you want your numbers to be accepted (entered) and then calculated average your logic is wrong within this code as it is STATIC (it will always give you a result as 110 and 11). There is no way now to have an input from the system (assume either it would be user input - let's say by using scanner and determining what array length of String going to be) or receiving by parameters length of the array. So to have an input assume this (for user input, as parameters in main can't be putted only args, as system input, if want parameters - think to use different method):

    [CODE REMOVED]

    Also re-think code of average, what is it calculating? Not average, as it is actually can be done by "arg.length*(arg.length+1)"
    Attachments Pending Approval Attachments Pending Approval
    Last edited by GregBrannon; September 13th, 2014 at 01:57 AM. Reason: Spoon feedimg

  10. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Pls help out with this code...What is wrong with it?

    @skuch89: Please post your code correctly using code or highlight tags which are explained near the top of this link. But don't post code if it can be used by the OP as a solution as explain in the article, "The Problem with Spoon-feeding."

  11. #11
    Junior Member
    Join Date
    Dec 2013
    Location
    London
    Posts
    11
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Pls help out with this code...What is wrong with it?

    yes, sorry about wrongly posting code. Well I believe I don't do any spoon feeding, there is a lot to be working around this code as user never really explained what he is needed...
    *** code removed
    I believe there is logic fault in this code, as now it is useless... But however compiles and works
    Last edited by Norm; September 13th, 2014 at 08:46 AM. Reason: Please don't do OPs work for him

Similar Threads

  1. [SOLVED] What is wrong with my code? pls help!
    By nikolaiL in forum What's Wrong With My Code?
    Replies: 21
    Last Post: May 10th, 2014, 04:52 PM
  2. Replies: 3
    Last Post: November 26th, 2013, 06:41 AM
  3. Getting Error in code pls help
    By gokul1242 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 18th, 2011, 08:35 AM
  4. Could some pls help me out with this code??
    By Pearl in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 19th, 2011, 11:01 AM
  5. Pls help me to debug this code
    By coen1604 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 24th, 2011, 10:00 AM