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

Thread: calcAverage() not calculating properly

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default calcAverage() not calculating properly

    I have a program that takes in 5 test scores, averages them and then assigns a letter grade to the average. I can't seem to get it to calculate properly. Can anyone point me in the right direction?

    Here is the method used for averaging:
        public double calcAverage() 
        {
            //calculate the average testscore
            double average = 0;
     
            for (int i=0; i<nums.length;i++)
            {
                average+=nums[i];
                average/=nums.length;
            }
     
            //return the average score
            return average;
        }

    Here is where I have the button event:
    for(int i=0; i<txtNums.length; i++)
                {
                    scan = new Scanner(txtNums[i].getText());
                    if(scan.hasNextDouble())
                    {
                        nums[i] = scan.nextDouble();
                        output = calcAverage();
                    }
                    else
                    {
                        nums[i] = 0;
                        txtNums[i].setText(" Error ");
                        error = true;
                    }
                }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: calcAverage() not calculating properly

    How do you calculate the average without a computer?

    I recommend stepping through your program with a debugger, or at least adding some print statements, to figure out exactly where the program's execution differs from what you expect. What is the value of average each iteration of the loop?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: calcAverage() not calculating properly

    I just figured out what my issue was. I am trying to figure out how to mark it as solved.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: calcAverage() not calculating properly

    You should be able to go to edit post (might have to open it in a new tab, that's a known forum bug) and mark it as solved. You might want to make a follow-up reply explaining what it was, that way anybody with similar problems will learn from what you did.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Help Calculating a tip
    By Sephyncloud in forum What's Wrong With My Code?
    Replies: 9
    Last Post: September 16th, 2013, 07:58 AM
  2. Calculating Pi to more digits?
    By sci4me in forum Java Theory & Questions
    Replies: 5
    Last Post: May 23rd, 2013, 01:41 PM
  3. Calculating Percentile
    By lakshmivaraprasad in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 7th, 2011, 08:26 PM
  4. Calculating Strings?
    By SkyAphid in forum Java Theory & Questions
    Replies: 2
    Last Post: August 30th, 2011, 09:38 PM
  5. question on calculating
    By meowCat in forum Java Theory & Questions
    Replies: 5
    Last Post: August 9th, 2010, 05:06 PM