0

I am trying to simply read a file completely filled with integers. There is one integer per line and I need to take all those integers and find the average. I have written code to do this yet it does not work for me.

However, when I sent it to my teacher she used BlueJ and it worked (I used IntelliJ and Eclipse). I have no idea what is wrong with my code, it just skips over my while loop. The file is both a .txt and .dat.

public class Reader {
    private int total, num;
    private static String file;

    public Reader(String f){
        num = 1;
        total = 0;
        file = f;
    }

    private void readData(){
        Scanner t;

        try {
            t = new Scanner(new File(file));
            while(t.hasNextInt()){
                total += t.nextInt();
                num++;
            }
        } catch (Exception e) {
            System.out.print("error" + e.getMessage());
        }
    }

    public double average(){
        readData();
        return (double)total/num;
    }

    public static void main(String[] args){
        Reader r = new Reader("numbers.dat");
        double average = r.average();
        System.out.print(average);
    }
}

The code works everywhere else just not on my computer

0 Answers0