import java.util.Scanner; import java.io.*; public class exampleRead { public static void main(String[] args) throws IOException { Scanner keyboard = new Scanner (System.in); int accumulator =0; /* for(int student=1; student<=2;student++) { for(int test=1;test<=4;test++) { System.out.println("Enter the grade for student number" + student + " for exam number " + test); accumulator +=keyboard.nextInt(); } } System.out.println("The average for all tests for all students is " + (double)accumulator/8.0); */ /* File fileForScores = new File("scores.txt"); Scanner input = new Scanner(fileForScores); while (input.hasNext()){ accumulator += input.nextInt(); } System.out.println("The average for all tests for all students is " + (double)accumulator/8.0); */ System.out.println("Enter the filename: "); Scanner board = new Scanner(System.in); String filename = board.nextLine(); File fileForScores = new File(filename); if(!fileForScores.exists()){ System.out.println("File not found, check that it's in the same place as class file"); System.exit(0); } Scanner input = new Scanner(fileForScores); double counter =0.0; while (input.hasNext()){ accumulator += input.nextInt(); counter ++; } System.out.println("The average for all tests for all students is " + (double)accumulator/counter); input.close(); } }