Results 1 to 3 of 3
  1. #1
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default more accurate average of array data

    I can calculate the average somewhat but not as close as I would like. I'm working with the average method.

    Java Code:
    package test;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.Scanner;
    
    public class Test {
       private Scanner read;
       private int numData;
       int[] data;
        
        // Open File
       private void openFile() {
           try {
               read = new Scanner(new File("File.txt")); // reading integers from a file
           }
           catch (IOException e) {
               System.out.println("Could not open file input");
           }
       }
       
       //Close File
       private void closeFile() {
           read.close();
       }
      
       //Read File and Store it in an array of size numData
       private int[] readInputFile() {
           openFile();
           numData = read.nextInt(); // Creating size of array from file
           read.nextLine();
           data = new int[numData]; //Storing integers in an array
            for (int i = 0; i < numData; i++) {
                data[i] = read.nextInt();
                //System.out.printf("%s\n", data[i] + " array item: " +  i); //testing
            }
           closeFile();
           return data;
       }
       
       private void average() {
           data = readInputFile(); //array created from file 
           int num = 0;
           for (int i = 0; i < (data.length - 1); i++) { //data.length - 1 because the first number in file 
               num = num + data[i];                         //crates the size of the array (I think that is correct)
           }
           System.out.println("Average: " + num/(data.length - 2)); 
       }
        
       private void test() {
          average();
       }
       
        public static void main(String[] args) {
            new Test();
        }
        
        public Test() {
            test();
       }
    }
    when I calculate the average on a calculater I get 119.1666............7. The program displays the int value of 120 which doesn't seem right. Do I need to do some sort of conversions or something?

  2. #2
    .paul. is offline Member
    Join Date
    Jun 2012
    Posts
    73
    Blog Entries
    1
    Rep Power
    0

    Default Re: more accurate average of array data

    the int datatype is integer which are whole numbers
    try using float datatype instead...

  3. #3
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default Re: more accurate average of array data

    Ok, This is better

    Java Code:
    private void average() {
           data = readInputFile(); //array created from file 
           double num = 0;
           for (int i = 0; i < (data.length); i++) { 
               num = num + data[i];
           }
           System.out.println("Average: " + num/(data.length - 1)); //data.length - 1 because the first number in file crates the size of the array
       }
    float was better but double matches the value of the calculator.

Similar Threads

  1. Random Array with average
    By keki in forum New To Java
    Replies: 7
    Last Post: 04-24-2012, 06:14 PM
  2. Average of an array. Please help
    By vika in forum New To Java
    Replies: 1
    Last Post: 03-28-2011, 10:09 PM
  3. keep double accurate to two decimal places
    By africanhacker in forum New To Java
    Replies: 5
    Last Post: 03-24-2011, 07:23 PM
  4. 2 dimensional array, average student
    By aborgeld in forum Advanced Java
    Replies: 5
    Last Post: 03-05-2011, 04:12 PM
  5. Can't get correct average using array
    By nevets93 in forum New To Java
    Replies: 2
    Last Post: 02-11-2011, 11:33 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
  •