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

Thread: How do improve the accuracy of this averaging program

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

    Default How do improve the accuracy of this averaging program

    Hello,

    My first post, been following a set of tutorials online. And it had been writing an averager, were the number of values to be summed was defined in the code. I wanted this to be "user" set. So I attempted to give it a go. I got quite far through it and ran this. It was at that moment i realized I had an inaccuracy because i had absorbed each number as an int type. What i would like to do is to swap it out so that the program can output a decimal. I understand that this will mean i will have to use floats and doubles. But no idea how because most of what I've done has been copied (/tweaked) from examples within the course.

    If anyone could help me alter this code so that it will handle decimal places and output decimal results that would be a great help.

    Alge4

    package averager;
    import java.util.ArrayList;
    import javax.swing.JOptionPane;
     
    public class Averager {
        public static void main(String[] args) {
            //Declare variables
            ArrayList<Integer> list = new ArrayList<Integer>() ;
            int Number, i;
            int total =0;
            int average = 0;
            Number =  Integer.parseInt(JOptionPane.showInputDialog("Please the total amount of numbers to be averaged."));
            int[] grid = new int[Number];
            i =0;
            while(i < Number ){
               list.add(Integer.parseInt(JOptionPane.showInputDialog("Please enter number" + (i+1) + "of the numbers to be averaged.")));
               i++;
            }
            for(i=0;i<Number;i++){
                grid[i]= list.get(i);
     
            }
            for(i=0;i<Number;i++){
                total = grid[i]+total;
            }
            average= total/Number;
            JOptionPane.showMessageDialog(null,"The average of your "+Number+" Number(s) is: "+average);
     
        }
    }


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How do improve the accuracy of this averaging program

    It was at that moment i realized I had an inaccuracy because i had absorbed each number as an int type.
    Start there. Then work on upgrading the math and the output

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: How do improve the accuracy of this averaging program

    You can still have all the values as ints. When it comes to calculating the average you need at least one floating point value.

    9 / 2 = int
    9.0 / 2 = double
    9 / 2.0 = double
    9.0 / 2.0 = double

    Easiest solution for you would be to have the denominator as a double.
    Improving the world one idiot at a time!

Similar Threads

  1. How can I improve on this?
    By Erko in forum What's Wrong With My Code?
    Replies: 7
    Last Post: August 3rd, 2012, 02:42 PM
  2. Quick question (intergers and accuracy)
    By petemyster in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 17th, 2011, 06:22 PM
  3. This program works but Want to improve
    By SHStudent21 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 8th, 2011, 05:53 PM
  4. Averaging Numbers in an Array Based on References
    By aussiemcgr in forum Java Theory & Questions
    Replies: 6
    Last Post: August 6th, 2010, 06:39 PM
  5. Grade averaging program with array and multiple methods.
    By jeremykatz in forum What's Wrong With My Code?
    Replies: 8
    Last Post: November 9th, 2009, 09:44 PM